OOP Tkinter 框架内的框架

Frame inside A OOP Tkinter Frame

对,所以我正在尝试将一个框架放入服务员页面以将其拆分为不同的框架 喜欢这个设计,但没有任何效果。

这是设计:

我试图在里面创建一个基本的 Frame 但它没有出现。 我创建的 Frame 不会引发错误,但它可能处于不同的位置,所以我尝试移动它,但它没有改变任何东西,只是没有在 WaiterPage 上显示。

注意没有登录验证所以选择WaiterPage后点击登录即可。

import tkinter as tk
from tkinter import SUNKEN, Label, ttk
from tkinter import IntVar
from tkinter.constants import BOTH, BOTTOM, CENTER, GROOVE, LEFT, RIGHT
from typing import Container

class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self,*args,**kwargs)

        self.bg = tk.PhotoImage(file="D:/talha\Documents\Projects For Portfolio\Some Fun\CourseWork\Testbg.png")

        container = tk.Frame(self)
        self.geometry("800x500")
        self.resizable(False,False)

        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for page in (ManagerPage, WaiterPage, Login):
            frame = page(container,self)
            self.frames[page] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(page)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class WaiterPage(tk.Frame):
    def __init__(self, parent, controller):
        MainFrame = tk.Frame.__init__(self, parent)
        RightFrame = tk.Frame(MainFrame, background='blue')


class ManagerPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        tk.Label(self, text="Manager Page:").pack()

        LeftFrame = tk.Frame(self)
        LeftFrame.pack(side=LEFT)

        CurrentTables = tk.Listbox(LeftFrame, width=70,height=33).pack(side=LEFT,fill=BOTH)

        AddTable = ttk.Button(self, text="Add Table").place(width=160,height=37,relx=0.65, rely=0.5, anchor=CENTER)
        AddBooking = ttk.Button(self, text="Add Booking").place(width=160,height=37,relx=0.875, rely=0.5, anchor=CENTER)
        ViewBooking = ttk.Button(self, text="View Booking").place(width=160, height=37,relx=0.65, rely=0.65, anchor=CENTER)
        Collection = ttk.Button(self, text="Collection").place(width=160,height=37,relx=0.875, rely=0.65, anchor=CENTER)
        Inventory = ttk.Button(self, text="View Inventory").place(width=160,height=37,relx=0.75, rely=0.8, anchor=CENTER)
        Exit = ttk.Button(self, text="Exit").place(width=160,height=37,relx=0.75, rely=0.9, anchor=CENTER)


class Login(tk.Frame):
    def __init__(self, parent, controller):

        def CallBack():
            if ManagerValue.get() == 1:
                WaiterCheck.configure(state='disabled')

            if WaiterValue.get() == 1:
                ManagerCheck.configure(state='disabled')

            if ManagerValue.get() == 0:
                WaiterCheck.configure(state='normal')

            if WaiterValue.get() == 0:
                ManagerCheck.configure(state='normal')

        def CheckPage():
            if ManagerValue.get() == 1:
                self.controller.show_frame(ManagerPage)
            if WaiterValue.get() == 1:
                self.controller.show_frame(WaiterPage)

        tk.Frame.__init__(self,parent)
        self.controller = controller

        label_bkgr = tk.Label(self, image=self.controller.bg)
        label_bkgr.place(relx=0.5, rely=0.5, anchor=CENTER)

        tk.Label(self, text="Username: ",font=("Segoe UI", 12),bg='#59C8E3').place(relx=0.3, rely=0.35, anchor=CENTER)
        tk.Label(self, text="Password: ",font=("Segoe UI", 12),bg='#59C8E3').place(relx=0.3, rely=0.45, anchor=CENTER)

        ManagerValue = IntVar()
        WaiterValue = IntVar()

        ManagerCheck = tk.Checkbutton(self, text="Manager",variable=ManagerValue,command=CallBack,font=("Segoe UI", 12),bg='#59C8E3',activebackground='#59C8E3')
        ManagerCheck.place(relx=0.43, rely=0.535, anchor=CENTER)
        WaiterCheck = tk.Checkbutton(self, text="Waiter",variable=WaiterValue,command=CallBack,font=("Segoe UI", 12),bg='#59C8E3',activebackground='#59C8E3')
        WaiterCheck.place(relx=0.59, rely=0.535, anchor=CENTER)

        UserEntry = ttk.Entry(self)
        UserEntry.place(width=160,
        height=37,relx=0.5, rely=0.35, anchor=CENTER)

        PassEntry = ttk.Entry(self)
        PassEntry.configure(show="*")
        PassEntry.place(width=160,
        height=37,relx=0.5, rely=0.45, anchor=CENTER)

        Submit = ttk.Button(self, text="Submit",command=CheckPage)
        Submit.place(width=160,
        height=37,relx=0.5, rely=0.6, anchor=CENTER)

app = App()
app.mainloop()

第一个问题是你从来没有在RightFrame上调用packgrid,所以它永远不会出现。

第二个问题是 RightFrame 需要是 self 的 child 因为 MainFrameNone.

class WaiterPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        RightFrame = tk.Frame(self, background='blue')
        RightFrame.pack(fill="both", expand=True)

我不知道 pack(fill="both", expand=True) 是否是正确的选项,但关键是你必须在框架上调用 packgridplace让它可见。