我想在一个屏幕中有一个可重用的框架,其中包含小部件(稍后我将在另一个屏幕中重用该框架)。该框架将是屏幕的一部分,还将有其他小部件。
我尝试为可重用框架创建一个类(类FrameBovenverdieping(tk.Frame):),该类应显示在ViewBezettingBoven屏幕中。但是,FrameBovenverdieping显示在每个屏幕中,也显示在StartPage和PageTwo中。我只想在ViewBezettingBoven中拥有它。
此外,当我使用ViewBezettingBoven时,切换屏幕的按钮不再起作用
import tkinter as tk
PLATTEGROND_BOVEN_WIDTH = 1000
PLATTEGROND_BOVEN_HEIGHT = 720
PLATTEGROND_BENEDEN_WIDTH = 1000
PLATTEGROND_BENEDEN_HEIGHT = 720
class BezettingApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.geometry("{0}x{1}+0+0".format(self.winfo_screenwidth(), self.winfo_screenheight()))
self.bind("<Escape>", lambda e: e.widget.quit())
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, ViewBezettingBoven, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Page")
label.pack(pady=10,padx=10)
button = tk.Button(self, text="Visit Page 1",
command=lambda: controller.show_frame(ViewBezettingBoven))
button.pack()
button2 = tk.Button(self, text="Visit Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class ViewBezettingBoven(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!")
self.parent = parent
self.window = FrameBovenverdieping(self)
label.pack(pady=10,padx=10)
self.widgets()
def widgets(self):
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class FrameBovenverdieping(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.widgets()
def widgets(self):
photo = tk.PhotoImage(file='autplacement.png')
w = PLATTEGROND_BENEDEN_WIDTH
h = PLATTEGROND_BENEDEN_HEIGHT
imagelabel = tk.Label(image=photo)
imagelabel.image = photo
imagelabel.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two!!!")
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page One",
command=lambda: controller.show_frame(ViewBezettingBoven))
button2.pack()
app = BezettingApp()
app.mainloop()
输出:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Lonneke Kolkman\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Lonneke Kolkman\Google Drive\Assortimens\BezettingAppPy\bezettingapp.py", line 74, in <lambda>
command=lambda: controller.show_frame(StartPage))
NameError: name 'controller' is not defined
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Lonneke Kolkman\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Lonneke Kolkman\Google Drive\Assortimens\BezettingAppPy\bezettingapp.py", line 78, in <lambda>
command=lambda: controller.show_frame(PageTwo))
NameError: name 'controller' is not defined
>>>
程序运行,但是尽管我只希望它在ViewBezettingBoven中显示,但在每个页面中都显示可重复使用的框架。
答案 0 :(得分:0)
您的控制器变量超出范围。如果您将控制器发送给该函数,则我可以运行代码而不会出错:
class ViewBezettingBoven(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!")
self.parent = parent
self.window = FrameBovenverdieping(self)
label.pack(pady=10,padx=10)
#send controller to function
self.widgets(controller)
def widgets(self,controller):
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.pack()