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?
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()
CodePudding user response:
you can use:
your_frame_name.destroy()
to destroy the frame
or
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
CodePudding user response:
I know I need to somehow destroy the frame to reload the data.
First, change the __init__
to show only the starting page. Also, you can create a dictionary to map page names to page classes.
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:
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)
CodePudding user response:
You can:
- destroy current shown frame (need an instance variable to hold it)
- create the frame when it is to be shown
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")