在tkinter中如何在两个框架之间切换?

129

我已经按照教程建立了一些带有漂亮小 GUI 的脚本,但是它们都没有解决如何处理更复杂的程序。

如果你有一个带有“开始菜单”的开屏界面,并且在用户选择后移动到程序的不同部分并适当地重新绘制屏幕,那么最优雅的处理方式是什么?

是直接 .destroy() “开始菜单”框架然后创建一个带有另一部分小部件的新框架?当用户按下返回按钮时再反转这个过程吗?

4个回答

239

一种方法是将多个框架垂直堆叠在一起,然后可以通过改变它们的层叠顺序来使其中一个框架显示在另一个框架的上方。最上面的那个框架会被显示出来。如果所有的框架大小都相同,这样做的效果最好,但是稍加努力也可以处理任意大小的框架。

注意:为了使这种方法奏效,每个页面的小部件都必须以该页面 (即 self) 或其后代为父级 (或主控,取决于您喜欢的术语)。

以下是一个有些矫揉造作的例子,以展示这个概念:

try:
    import tkinter as tk                # python 3
    from tkinter import font as tkfont  # python 3
except ImportError:
    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")

随着时间的推移,人们使用这段代码(或者仿照这段代码的在线教程)作为起点提出了其他问题。您可能想要阅读这些问题的答案:


5
哇,非常感谢。这只是一个学术问题,而不是实际应用问题。在这些页面互相重叠的情况下,需要多少页才会开始变得卡顿和无响应呢? - Max Tilley
7
我不知道。可能有数千个。测试起来很容易。 - Bryan Oakley
3
肯定有更简单的方法来实现这个,可以使用或不使用堆叠框架。在最简单的情况下,创建一个带有所需内容的框架。当你准备好切换时,销毁该框架并创建其他内容即可。 - Bryan Oakley
3
是的,如果你正在使用pack,那么可以使用pack_forget - Bryan Oakley
4
警告:用户可能通过按Tab键选择“隐藏”的背景框架上的小部件,然后使用Enter键激活它们。请注意。 - Stevoisiak
显示剩余7条评论

44

这里有另一个简单的答案,但不使用类。

from tkinter import *


def raise_frame(frame):
    frame.tkraise()

root = Tk()

f1 = Frame(root)
f2 = Frame(root)
f3 = Frame(root)
f4 = Frame(root)

for frame in (f1, f2, f3, f4):
    frame.grid(row=0, column=0, sticky='news')

Button(f1, text='Go to frame 2', command=lambda:raise_frame(f2)).pack()
Label(f1, text='FRAME 1').pack()

Label(f2, text='FRAME 2').pack()
Button(f2, text='Go to frame 3', command=lambda:raise_frame(f3)).pack()

Label(f3, text='FRAME 3').pack(side='left')
Button(f3, text='Go to frame 4', command=lambda:raise_frame(f4)).pack(side='left')

Label(f4, text='FRAME 4').pack()
Button(f4, text='Goto to frame 1', command=lambda:raise_frame(f1)).pack()

raise_frame(f1)
root.mainloop()

39
注意: 根据JDN96的说法, 下面的答案可能会导致内存泄漏,因为它反复销毁和重新创建框架。但是,我自己没有测试验证过。
tkinter中切换框架的一种方法是销毁旧框架,然后用新框架替换它。
我改编了Bryan Oakley的答案,在替换之前销毁旧框架。作为额外的奖励,这消除了对container对象的需要,并允许您使用任何通用的Frame类。
# Multi-frame tkinter application v2.3
import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartPage)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

class StartPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is the start page").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Open page one",
                  command=lambda: master.switch_frame(PageOne)).pack()
        tk.Button(self, text="Open page two",
                  command=lambda: master.switch_frame(PageTwo)).pack()

class PageOne(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page",
                  command=lambda: master.switch_frame(StartPage)).pack()

class PageTwo(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page two").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page",
                  command=lambda: master.switch_frame(StartPage)).pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

Start page Page one Page two

解释

switch_frame()的工作原理是接受任何实现Frame的类对象。该函数然后创建一个新的帧来替换旧的帧。

  • 如果存在旧的_frame,则删除它,然后用新帧替换它。
  • 使用.pack()添加的其他框架(如菜单栏)将不受影响。
  • 可与任何实现tkinter.Frame的类一起使用。
  • 窗口会自动调整大小以适应新内容

版本历史

v2.3

- Pack buttons and labels as they are initialized

v2.2

- Initialize `_frame` as `None`.
- Check if `_frame` is `None` before calling `.destroy()`.

v2.1.1

- Remove type-hinting for backwards compatibility with Python 3.4.

v2.1

- Add type-hinting for `frame_class`.

v2.0

- Remove extraneous `container` frame.
    - Application now works with any generic `tkinter.frame` instance.
- Remove `controller` argument from frame classes.
    - Frame switching is now done with `master.switch_frame()`.

v1.6

- Check if frame attribute exists before destroying it.
- Use `switch_frame()` to set first frame.

v1.5

  - Revert 'Initialize new `_frame` after old `_frame` is destroyed'.
      - Initializing the frame before calling `.destroy()` results
        in a smoother visual transition.

v1.4

- Pack frames in `switch_frame()`.
- Initialize new `_frame` after old `_frame` is destroyed.
    - Remove `new_frame` variable.

v1.3

- Rename `parent` to `master` for consistency with base `Frame` class.

v1.2

- Remove `main()` function.

v1.1

- Rename `frame` to `_frame`.
    - Naming implies variable should be private.
- Create new frame before destroying old frame.

v1.0

- Initial version.

2
@quantik 溢出效应发生是因为旧按钮没有被正确地销毁。请确保您将按钮直接附加到框架类(通常为 self)。 - Stevoisiak
1
@Deepworks 我没有考虑到多个类文件。不幸的是,多个文件可能容易出现circular dependencies问题。 - Stevoisiak
16
我建议删除这个回答中的版本历史部分,它是完全无用的。如果您想查看历史记录,stackoverflow 提供了一个机制来实现。大多数阅读此答案的人不会关心历史记录。 - Bryan Oakley
3
非常感谢您提供版本历史记录。了解到您随着时间的推移更新和修订了答案,这是非常好的。 - Vasyl Vaskivskyi
2
@StevoisiaksupportsMonica:链接的内存泄漏Perl/Tk子系统有关。这也会在使用Python时发生,没有任何地方被提交。 - stovfl
显示剩余7条评论

3
也许更直观的解决方案是,如果您使用的是pack几何管理器,则使用pack_forget方法来隐藏/显示帧。
以下是一个简单的示例。
import tkinter as tk


class App:
    def __init__(self, root=None):
        self.root = root
        self.frame = tk.Frame(self.root)
        self.frame.pack()
        tk.Label(self.frame, text='Main page').pack()
        tk.Button(self.frame, text='Go to Page 1',
                  command=self.make_page_1).pack()
        self.page_1 = Page_1(master=self.root, app=self)

    def main_page(self):
        self.frame.pack()

    def make_page_1(self):
        self.frame.pack_forget()
        self.page_1.start_page()


class Page_1:
    def __init__(self, master=None, app=None):
        self.master = master
        self.app = app
        self.frame = tk.Frame(self.master)
        tk.Label(self.frame, text='Page 1').pack()
        tk.Button(self.frame, text='Go back', command=self.go_back).pack()

    def start_page(self):
        self.frame.pack()

    def go_back(self):
        self.frame.pack_forget()
        self.app.main_page()


if __name__ == '__main__':
    root = tk.Tk()
    app = App(root)
    root.mainloop()

enter image description here

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接