[英]Tkinter have stacking frames with list and pages in different modules
I have code below originally taken from the link here i have read all documentation in there but i feel im overlooking something, i have pages in separate modules as well as a separate module with a Add class for adding pages to the window.我有下面的代码最初取自这里的链接 我已经阅读了那里的所有文档,但我觉得我忽略了一些东西,我在单独的模块中有页面以及一个带有 Add 类的单独模块,用于将页面添加到窗口。
The idea is to later be able to drop a module in a sub folder with a Pagexxx object within it and call the add page class to allow Tkinter to display it but i cannot seem to get the frames to stack.这个想法是以后能够将一个模块放入一个子文件夹中,其中包含一个 Pagexxx 对象,并调用添加页面类以允许 Tkinter 显示它,但我似乎无法将框架堆叠起来。
Nav.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()
Page.py页面.py
class PageList():
frames = {}
def __init__(self, name, parent, cont, ref):
self.frames[name] = ref(parent=parent, controller=cont)
Mypages.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("Page1"))
button1.pack()
Before asking for help from others, the first step is to validate your assumptions.在向他人寻求帮助之前,第一步是验证您的假设。 You're assuming that tk.Frame.__init__(self, parent)
returns something useful, but you never validated that assumption by checking to see if it is what you think it is.您假设tk.Frame.__init__(self, parent)
返回一些有用的东西,但您从未通过检查它是否是您认为的那样来验证该假设。
The first problem is illustrated by two lines, which are essentially the same in both pages:第一个问题用两行来说明,两页的内容基本相同:
this = tk.Frame.__init__(self, parent)
label = tk.Label(this, text="Welcome to Page 1")
I'm guessing you were assuming that this
would be set to the instance of the Frame
.我猜你是假设this
将被设置为Frame
的实例。 However, the __init__
function returns None
so this
is set to None
.但是, __init__
函数返回None
所以this
被设置为None
。 When None
is passed as the parent of a widget, that widget becomes a child of the root window.当None
作为小部件的父级传递时,该小部件将成为根窗口的子级。
The solution is to not use this
.解决方案是不使用this
。 Use self
:使用self
:
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Welcome to Page 2")
The second problem is that you never add the page to the container with grid
, pack
, or place
, so they will never be visible.第二个问题是您永远不会将页面添加到带有grid
、 pack
或place
的容器中,因此它们将永远不可见。
You need to change Add
to actually add the page to the container:您需要更改Add
以将页面实际添加到容器中:
def Add(name, parent, cont, ref):
PageList.frames[name] = ref(parent=parent, controller=cont)
PageList.frames[name].grid(row=0, column=0, sticky="nsew")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.