python - Tkinter 有单独文件中页面的代码

标签 python python-2.7 tkinter

我的代码可以打开一个窗口,并具有用于第 1 页、第 2 页和第 3 页的三个按钮。但是我还有四个 .py 文件(Dashboard、PageOne、PageTwo、PageThree)。仪表板将是用于运行应用程序的文件。我想要它,以便每个文件的代码仅在用户单击相应的页面按钮时运行/显示。

仪表板.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

最佳答案

错误消息会告诉您解决问题所需的一切信息。这是一个与 tkinter 无关的简单问题,与正确组织和使用代码有关。

例如,Page is not defined 告诉你什么?它告诉您,从字面上看,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):
    ...

仪表盘.py
from pageOne import PageOne
from pageTwo import PageTwo
from pageThree import PageThree
...

关于python - Tkinter 有单独文件中页面的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39530107/

相关文章:

python - "Pandorable"在数据帧切片中返回索引的方法

python - 如何在pygame中防止垃圾邮件子弹

python - 使用正则表达式搜索 tkinter 文本小部件内容

python - 准备一个可供国际使用的网站

python - 查找二维列表中特定列的长度

python-2.7 - 如何获取 Python 字典值列表并在 Kivy UI 中显示为表格(使用 ListView 小部件)?

python - 给自己还是不给自己?

python - 如何使用 scrapy.Request 将另一个页面的元素加载到项目中

python - 使用类绘制星形?

python-3.x - 如何选择所有 TreeView 并使用 ctrl-c 复制