Tkinter有用于在单独文件中创建页面的代码。

4
我有一段代码,它打开一个窗口,并有三个按钮,分别对应第一页、第二页和第三页。但是我还有四个.py文件(Dashboard,PageOne,PageTwo,PageThree)。Dashboard是运行应用程序的文件。我希望每个文件的代码只在用户单击相应页面按钮时运行/显示。
Dashboard.py:
import Tkinter as tk
import PageOne
import PageTwo
import PageThree

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)
        b3 = tk.Button(buttonframe, text="Page 3", command=p3.lift)

        b1.pack(side="left")
        b2.pack(side="left")
        b3.pack(side="left")

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("400x400")
    root.mainloop()

PageOne.py:

import Tkinter as tk
import Dashboard

class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 1")
       label.pack(side="top", fill="both", expand=True)

PageTwo.py

import Tkinter as tk
import Dashboard

class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 2")
       label.pack(side="top", fill="both", expand=True)

PageThree.py:

import Tkinter as tk
import Dashboard

class Page3(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 3")
       label.pack(side="top", fill="both", expand=True)

我遇到的错误是:

Traceback (most recent call last):
  File "C:\Users\ross.watson\Google Drive\Smart Mirror\Test\Dashboard.py", line 2, in <module>
    import PageOne
  File "C:\Users\ross.watson\Google Drive\Smart Mirror\Test\PageOne.py", line 2, in <module>
    import Dashboard
  File "C:\Users\ross.watson\Google Drive\Smart Mirror\Test\Dashboard.py", line 3, in <module>
    import PageTwo
  File "C:\Users\ross.watson\Google Drive\Smart Mirror\Test\PageTwo.py", line 4, in <module>
    class Page2(Page):
NameError: name 'Page' is not defined

1
不太确定你的意思,但答案是否简单,只需要将每个页面放在单独的文件中(例如page1.py),并使用“from page1 import Page1”即可。 - scotty3785
2
将类Page放置在它自己的文件Page.py中,并在每个PageX.py文件中使用'from Page import Page'。 - scotty3785
1
同样在Dashboard.py中,您需要使用p1 = PageOne.Page1(self)来初始化页面类。 - scotty3785
1
如果您正在尝试重新创建选项卡笔记本效果,则应该直接使用内置的ttk.Notebook。.lift方法旨在将窗口置于前台,而不是框架小部件。 - scotty3785
@scotty3785说:" .lift方法的设计目的是将窗口置于前面而不是框架小部件",这并不是一个正确的说法。lift命令绝对是旨在改变所有小部件的堆叠顺序。然而,你建议使用ttk笔记本是很好的。 - Bryan Oakley
显示剩余7条评论
1个回答

7
错误信息会告诉你如何解决问题。这是一个简单的问题,与tkinter无关,而是与正确组织和使用代码有关。例如,“Page未定义”告诉你什么?它确切地告诉你Page未定义。你在主文件中定义了它,但在另一个文件中使用了它。解决方案是将Page的定义移动到一个可以被引入到使用它的文件中的单独文件中。
修改你的文件和引用看起来像这样:

page.py

class Page(tk.Frame):
    ...

pageOne.py

from page import Page
class PageOne(Page):
    ...

pageTwo.py

from page import Page
class PageTwo(Page):
    ...

pageThree.py

from page import Page
class PageThree(Page):
    ...

Dashboard.py

from pageOne import PageOne
from pageTwo import PageTwo
from pageThree import PageThree
...

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