在tkinter中为每个页面使用单独的文件在两个帧之间切换

时间:2018-06-26 10:58:17

标签: python python-3.x tkinter python-import

我创建了一个tkinter应用程序,它具有三个页面:StartPage,PageOne和PageTwo。从StartPage,您可以转到PageOne和PageTwo,而从PageOne和PageTwo,您可以返回StartPage。为了实现它,我使用的是史蒂芬·瓦斯切拉罗(Steven Vascellaro)在本文Switch between two frames in tkinter中描述的技术。

这是史蒂文(Steven)提供的代码:

# Multi-frame tkinter application v2.2
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)

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


class PageOne(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

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


class PageTwo(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

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


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

现在,我正在尝试将此代码模块化,并将每个页面放置在单独的python文件中。这就是我如何在三个Python文件(start_page.py,page_one.py,page_two.py)之间拆分Steven的代码的方法:

start_page.py:

import tkinter as tk
from page_one import PageOne
from page_two import PageTwo


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)

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


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

page_one.py:

import tkinter as tk
from start_page import StartPage

class PageOne(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

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

page_two.py:

import tkinter as tk
from start_page import StartPage

class PageTwo(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

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

问题出在这里,类StartPage依赖于PageOne和PageTwo,而PageOne和PageTwo依赖于StartPage。这会导致本文ImportError: Cannot import name X中描述的错误。

tkinter中是否有一种方法可以保留所描述的功能,但允许每个页面位于单独的python文件中?

0 个答案:

没有答案