import tkinter as tk
LARGE_FONT=("Verdana",12)
class myClass(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
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 = {}
frame = StartPage(container,self,"startPage","visit page 1","pageone")
frame2 = StartPage(container,self,"pageone","Go BAck","startPage")
self.frames["startPage"]=frame
self.frames["pageone"]=frame2
frame.grid(row=0,column=0,sticky="nsew")
self.show_frame("startPage")
def show_frame(self,cont):
print(cont)
print(self.frames)
print(self.frames[cont])
frame = self.frames[cont]
print(frame)
frame.tkraise()
def qf(param):
print(param)
class StartPage(tk.Frame):
def __init__(self,parent,controller,lableName,butonName,visitToPage):
tk.Frame.__init__(self,parent)
label = tk.Label(self,text=lableName,font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self,text =butonName,command=lambda: controller.show_frame(visitToPage))
button1.pack()
app = myClass()
app.mainloop()
我正在尝试使用一个类StartPage创建两个框架,所以我给第一帧的StartPage提供了参数“startPage”,“visit page 1”,“pageone” 而对于frame2是“pageone”,“Go BAck”,“startPage”
但即使参数对于show_frame
函数是正确的,按下按钮框也不会切换答案 0 :(得分:5)
它没有工作,因为您还没有将pageone框架放在网格中。只需将此行放在frame.grid之后就可以了:
frame2.grid(row=0,column=0,sticky="nsew")