Python-在tkinter的两个帧之间切换

正如教程向我展示的那样,我已经在最初的几个脚本上构建了一个不错的GUI,但是没有一个脚本解决更复杂的程序的问题。

如果在打开屏幕上有带有“开始菜单”的内容,并且在用户选择后移动到程序的其他部分并适当地重画屏幕,那么执行此操作的优雅方法是什么?

是否只是.destroy()一个“开始菜单”框架,然后创建一个新的窗口小部件,并填充另一部分的小部件?并在他们按下“后退”按钮时逆转此过程?

回答:

一种方法是将框架堆叠在一起,然后可以按照堆叠顺序将一个框架升高到另一个框架之上。最上面的一个将是可见的。如果所有框架的尺寸都相同,则效果最好,但是只需一点点工作,即可使其适用于任何尺寸的框架。

注意:为了使此功能起作用,页面的所有小部件都必须具有该页面(即:)self或后代作为父项(或母版,取决于你喜欢的术语)。

这是一个人为的例子,向你展示一般概念:

import tkinter as tk                # python 3

from tkinter import font as tkfont # python 3

#import Tkinter as tk # python 2

#import tkFont as tkfont # python 2

class 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")

以上是 Python-在tkinter的两个帧之间切换 的全部内容, 来源链接: utcz.com/qa/421823.html

回到顶部