1 回答

TA貢獻1757條經驗 獲得超7個贊
我建議重構這樣的東西。 現在將負責根據請求實例化新幀;你不必管理自己。show_frame()self.frames
class HeadlineGame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.container = 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 = {}
self.show_frame(StartPage)
def show_frame(self, controller):
if controller not in self.frames:
self.frames[controller] = frame = controller(self.container, self)
frame.grid(row=0, column=0, sticky="nsew")
frame = self.frames[controller]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
publishbutton = tk.Button(
self, image=publishimg, borderwidth=0, command=lambda: controller.show_frame(PublishPage)
)
publishbutton.image = publishimg
publishbutton.place(x=503, y=315)
class PublishPage(tk.Frame):
def __init__(self, parent, controller):
button2 = tk.Button(self, text="Test")
button2.grid(row=0, column=0)
app = HeadlineGame()
app.geometry("1366x768")
app.mainloop()
添加回答
舉報