编辑版本:
我是Tkinter的新手,我想执行一个类似于这个问题的答案(Switch between two frames in tkinter?)的页面切换功能,但是,我不想将所有页面放在一个单独的. py文件中,我想从每个页面自己的. py文件中调用每个页面。
我想要这个的原因是,以后我将有多个子页面,每个子页面将代表不同的工具。
我的问题是,
当我的每个页面都从不同的python文件中调用时,我如何仍然保持相同的帧切换功能?
以下是我迄今为止尝试过的方法:app.py
from page1 import *
import tkinter as tk
from tkinter import Tk, Frame, ttk
class AppWindow(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
# create a container for all the widgets (buttons, etc)
container = Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
# for loops for switching between pages
for F in (HomePage, PageOne, Tool1):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(HomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class HomePage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = ttk.Label(self, text="App Main Window", font=LARGE_FONT)
label.pack(pady=10, padx=10)
proc_btn = ttk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
proc_btn.pack(ipadx=5, ipady=5, expand=1)
if __name__ == "__main__":
app = AppWindow()
app.mainloop()
page1.py
from apptest import *
import tkinter as tk
from tkinter import Tk, Frame, ttk
class PageOne(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = ttk.Label(self, text="Page One !!!!!!!!", font=LARGE_FONT)
label.pack(pady=10, padx=10)
proc1_btn = ttk.Button(self, text="Tool 1",
command=lambda: controller.show_frame(Tool1))
proc1_btn.pack(ipadx=5, ipady=5, expand=1)
home_btn = ttk.Button(self, text="Homepage",
command=lambda: controller.show_frame(HomePage))
home_btn.pack(ipadx=5, ipady=5, expand=1)
class Tool1(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = ttk.Label(self, text="Tool 1 !!!!!!!!", font=LARGE_FONT)
label.pack(pady=10, padx=10)
# Find what
ttk.Label(self, text='Find:')
keyword = ttk.Entry(self, width=30)
keyword.focus()
keyword.pack(ipadx=5, ipady=5, expand=1)
# Replace with:
ttk.Label(self, text='Replace:')
replacement = ttk.Entry(self, width=30)
replacement.pack(ipadx=5, ipady=5, expand=1)
proc1_btn = ttk.Button(self, text="PageOne",
command=lambda: controller.show_frame(PageOne))
proc1_btn.pack(ipadx=5, ipady=5, expand=1)
home_btn = ttk.Button(self, text="Homepage",
command=lambda: controller.show_frame(HomePage))
home_btn.pack(ipadx=5, ipady=5, expand=1)
当我在"PageOne"和"Tool1"页面上尝试使用"HomePage"按钮返回主页时,出现问题。以下是我看到的错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "...\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "...\GUI\page1.py", line 21, in <lambda>
command=lambda: controller.show_frame(HomePage))
File "C.../GUI/app.py", line 52, in show_frame
frame = self.frames[cont]
KeyError: <class 'apptest.HomePage'>
提前感谢您的帮助
- 答案:**
这个问题的答案可以在这里找到:Switch between two frames in tkinter in separates files
1条答案
按热度按时间ncecgwcz1#
HomePage
类复制并删除到page1.py
模块。from apptest import *
。实际上,我不知道它是什么。开篇截图:
点击屏幕截图转至第一页:
点击屏幕截图,转至工具1:
截图点击首页: