Python Tkinter在单独的文件中有页面代码

Python Tkinter在单独的文件中有页面代码,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我有打开一个窗口的代码,有三个按钮,分别用于第1页、第2页和第3页。然而,我也有四个.py文件(Dashboard、PageOne、PageTwo.PageThree)。Dashboard将是用于运行应用程序的文件。我希望这样,每个文件中的代码只在用户单击相应的页面按钮时运行/显示 Dashboard.py: import Tkinter as tk import PageOne import PageTwo import PageThree class Page(tk.Frame):

我有打开一个窗口的代码,有三个按钮,分别用于第1页、第2页和第3页。然而,我也有四个.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)
第二页

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)
我得到的错误是:

回溯(最近一次呼叫最后一次):
文件“C:\Users\ross.watson\Google Drive\Smart Mirror\Test\Dashboard.py”,第2行,在
导入第一页
文件“C:\Users\ross.watson\Google Drive\Smart Mirror\Test\PageOne.py”,第2行,在
导入仪表板
文件“C:\Users\ross.watson\Google Drive\Smart Mirror\Test\Dashboard.py”,第3行,在
导入第二页
文件“C:\Users\ross.watson\Google Drive\Smart Mirror\Test\PageTwo.py”,第4行,在
班级第2页(第页):
名称错误:未定义名称“页面”

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

例如,什么是
页面没有定义
告诉您?它从字面上告诉你,
Page
没有定义。您在主文件中定义了它,但正在另一个文件中使用它。解决方案是将
页面的定义移动到一个单独的文件中,该文件可以导入到使用它的文件中

将文件和导入修改为如下所示:

page.py pageOne.py 第二页 第三页 Dashboard.py
不是100%确定你得到的是什么,但答案是否简单到将每个页面放在一个单独的文件(例如page1.py)中,并在剪切
类第3页(第页)时使用“from page1 import page1”?@scotty3785:
PageThree.py中
我得到
名称错误:在运行
Dashboard.py时未定义全局名称“Page1”
将类页面放在它自己的文件Page.py中,并在每个PageX.py文件中使用“from Page import Page”。在Dashboard.py中,还需要使用p1=PageOne.Page1(self)初始化page类。另外,如果您试图重新创建选项卡式笔记本效果,那么您可能只需要使用内置的ttk.notebook。.lift方法旨在将窗口置于前端,而不是框架小部件
class Page(tk.Frame):
    ...
from page import Page
class PageOne(Page):
    ...
from page import Page
class PageTwo(Page):
    ...
from page import Page
class PageThree(Page):
    ...
from pageOne import PageOne
from pageTwo import PageTwo
from pageThree import PageThree
...