【发布时间】:2019-12-30 20:11:29
【问题描述】:
使用最初发布的以下代码here 我如何使用带有PageList() 命令的第4 个模块和它下面的新页面类在我的WindowHandler() 类中显示它?通过其他研究,我相信您将不得不停止使用 Windowhandler() 作为主应用程序类的实例。
这样做的目的是让堆叠的帧在不同的文件中,而每个文件都可以更新frames={} 列表,而无需将其添加到主类中。 here 为不同的页面提供了类似的示例,但对这些页面的调用仍必须添加到主类中。
导航.py
import tkinter as tk
from Page import PageList
import Mypages
class Windowhandler(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand= True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
PageList("Page1", container, self, Mypages.PageOne)
PageList("Page2", container, self, Mypages.PageTwo)
self.show_frame("Page1")
def show_frame(self, cont):
frameref = PageList.frames[cont]
print(frameref)
frameref.tkraise()
app = Windowhandler()
app.mainloop()
页面.py
class PageList():
frames = {}
def __init__(self, name, parent, cont, ref):
self.frames[name] = ref(parent=parent, controller=cont)
我的页面.py
import tkinter as tk
class PageOne(tk.Frame):
def __init__(self, parent, controller):
this = tk.Frame.__init__(self, parent)
label = tk.Label(this, text="Welcome to Page 1")
label.pack(pady=10, padx=10)
button1 = tk.Button(this, text="Back to Home",
command=lambda: controller.show_frame("Page2"))
button1.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
this = tk.Frame.__init__(self, parent)
label = tk.Label(this, text="Welcome to Page 2")
label.pack(pady=10, padx=10)
button1 = tk.Button(this, text="Back to Home",
command=lambda: controller.show_frame("NewPage"))
button1.pack()
Psuedo.py
import tkinter as tk
import Nav
PageList("NewPage", Nav.container, WindowHandler, NewPage)
class NewPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
btn1 = Nav(self, text="A Button" command=lambda: Controller.show_frame("Page1"))
btn1.pack(pady=(10, 4))
【问题讨论】:
-
听起来您想创建一个插件系统,只需导入文件即可插入新页面。我说的对吗?
-
你是对的我有一个 importlib 设置,其中新文件被导入到我的主文件中,我希望每个新文件都扩展菜单,以便每个文件都有自己的菜单,是的,这可能是超过我的技能水平。
标签: python python-3.x tkinter