一种方法是将框架堆叠在一起,然后可以按照堆叠顺序将一个框架升高到另一个框架之上。最上面的一个将是可见的。如果所有框架的尺寸都相同,则效果最好,但是只需一点点工作,即可使其适用于任何尺寸的框架。
注意:为了使此功能起作用,页面的所有小部件都必须具有该页面(即:)
self或后代作为父项(或母版,取决于你喜欢的术语)。
这是一个人为的例子,向你展示一般概念:
import tkinter as tk # python 3from tkinter import font as tkfont # python 3#import Tkinter as tk # python 2#import tkFont as tkfont # python 2class SampleApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic") # the container is where we'll stack a bunch of frames # on top of each other, then the one we want visible # will be raised above the others 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, PageOne, PageTwo): page_name = F.__name__ frame = F(parent=container, controller=self) self.frames[page_name] = frame # put all of the pages in the same location; # the one on the top of the stacking order # will be the one that is visible. frame.grid(row=0, column=0, sticky="nsew") self.show_frame("StartPage") def show_frame(self, page_name): '''Show a frame for the given page name''' frame = self.frames[page_name] frame.tkraise()class StartPage(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="This is the start page", font=controller.title_font) label.pack(side="top", fill="x", pady=10) button1 = tk.Button(self, text="Go to Page One", command=lambda: controller.show_frame("PageOne")) button2 = tk.Button(self, text="Go to Page Two", command=lambda: controller.show_frame("PageTwo")) button1.pack() button2.pack()class PageOne(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="This is page 1", font=controller.title_font) label.pack(side="top", fill="x", pady=10) button = tk.Button(self, text="Go to the start page", command=lambda: controller.show_frame("StartPage")) button.pack()class PageTwo(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="This is page 2", font=controller.title_font) label.pack(side="top", fill="x", pady=10) button = tk.Button(self, text="Go to the start page", command=lambda: controller.show_frame("StartPage")) button.pack()if __name__ == "__main__": app = SampleApp() app.mainloop()如果在类中发现创建实例的概念令人困惑,或者在构造过程中不同的页面需要不同的参数,则可以分别显式调用每个类。该循环主要用于说明每个类都相同的观点。
例如,要单独创建类,可以删除循环
(for F in (StartPage, ...)使用以下命令:
self.frames["StartPage"] = StartPage(parent=container, controller=self)self.frames["PageOne"] = PageOne(parent=container, controller=self)self.frames["PageTwo"] = PageTwo(parent=container, controller=self)self.frames["StartPage"].grid(row=0, column=0, sticky="nsew")self.frames["PageOne"].grid(row=0, column=0, sticky="nsew")self.frames["PageTwo"].grid(row=0, column=0, sticky="nsew")



