[英]Python Tkinter Frame Destroy to display new data
App works fine when first loaded.应用程序在首次加载时运行良好。 I need to update data on each frame to display what the user has changed.我需要更新每一帧的数据以显示用户所做的更改。 I know I need to somehow destroy the frame to reload the data.我知道我需要以某种方式破坏框架以重新加载数据。 Any suggestions on how to edit switch_frame to destroy to previous frame?关于如何编辑 switch_frame 以破坏到上一帧的任何建议?
class App(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)
self.frames = {}
for F in (homepage, page2, page3):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.switch_frame("homepage")
def switch_frame(self, frame_class):
frame = self.frames[frame_class]
frame.tkraise()
you can use:您可以使用:
your_frame_name.destroy()
to destroy the frame your_frame_name.destroy()
销毁框架
or或者
your_label_or_something_other_like_button_name.config(text='you can change text or anyting other you want
to set new data your_label_or_something_other_like_button_name.config(text='you can change text or anyting other you want
to set new data
I hope this help you and feel free to ask again if you didnt understand something我希望这对您有所帮助,如果您不明白,请随时再次询问
I know I need to somehow destroy the frame to reload the data.我知道我需要以某种方式破坏框架以重新加载数据。
First, change the __init__
to show only the starting page.首先,更改__init__
以仅显示起始页。 Also, you can create a dictionary to map page names to page classes.此外,您可以创建一个字典,将 map 页面名称转换为页面类。
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.pages = {
"homepage": homepage,
"page2": page2,
"page3": page3,
}
self.container = tk.Frame(self)
self.container.pack(side="top", fill="both", expand="true")
self.switch_frame("homepage")
Next, modify switch_frame
to destroy the old contents of self.container
and then create a new instance of the requested page:接下来,修改switch_frame
以销毁self.container
的旧内容,然后创建请求页面的新实例:
def switch_frame(self, page_name):
children = self.container.winfo_children()
for child in children:
child.destroy()
frame_class = self.pages[page_name]
frame = frame_class(self.container, controller=self)
frame.pack(fill="both", expand=True)
You can:你可以:
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# used instance variable so that it can be accessed in other class functions
self.container = tk.Frame(self)
self.container.pack(side="top", fill="both", expand="true")
self.container.grid_rowconfigure(0, weight=1)
self.container.grid_columnconfigure(0,weight=1)
self.frames = {}
self.frame = None # hold the current frame shown
for F in (homepage, page2, page3):
page_name = F.__name__
self.frames[page_name] = F
self.switch_frame("homepage")
def switch_frame(self, frame_class):
# destroy current frame shown if any
if self.frame:
self.frame.destroy()
# create the required frame and show it
self.frame = self.frames[frame_class](self.container, self)
self.frame.grid(row=0, column=0, sticky="nsew")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.