Ros*_*oss 3 python tkinter python-2.7
我有打开一个窗口的代码,并且有第 1 页、第 2 页和第 3 页的三个按钮。但是我也有四个 .py 文件(仪表板、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()
Run Code Online (Sandbox Code Playgroud)
第一页.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)
Run Code Online (Sandbox Code Playgroud)
第二页.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)
Run Code Online (Sandbox Code Playgroud)
第三页.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)
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
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()
Run Code Online (Sandbox Code Playgroud)
错误消息会告诉您解决问题所需的一切信息。这是一个简单的问题,与 tkinter 无关,与正确组织和使用代码有关。
例如,Page is not defined告诉你什么?它告诉你,从字面上看,这Page没有定义。您在主文件中定义了它,但您正在另一个文件中使用它。解决方案是将 的定义移动Page到一个单独的文件中,该文件可以导入到使用它的文件中。
修改您的文件和导入,如下所示:
class Page(tk.Frame):
...
Run Code Online (Sandbox Code Playgroud)
from page import Page
class PageOne(Page):
...
Run Code Online (Sandbox Code Playgroud)
from page import Page
class PageTwo(Page):
...
Run Code Online (Sandbox Code Playgroud)
from page import Page
class PageThree(Page):
...
Run Code Online (Sandbox Code Playgroud)
from pageOne import PageOne
from pageTwo import PageTwo
from pageThree import PageThree
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3695 次 |
| 最近记录: |