Function To Switch Between Two Frames In Tkinter
I'm looking through the code at passing-functions-parameters-tkinter-using-lambda, and needed a tad more functionality inside his class PageOne(tk.Frame). Instead of using lambda
Solution 1:
You shouldn't put any logic in a lambda. Just create a normal function that has any logic you want, and call it from the button. It's really no more complicated that that.
classSomePage(...):
def__init__(...):
...
button1 = tk.Button(self, text="Back to Home",
command=lambda: self.maybe_switch_page(StartPage))
...
defmaybe_switch_page(self, destination_page):
if ...:
self.controller.show_frame(destination_page)
else:
...
If you want a more general purpose solution, move the logic to show_frame
, and have it call a method on the current page to verify that it is OK to switch.
For example:
classController(...):
...
defshow_frame(self, destination):
if self.current_page isNoneor self.current_page.ok_to_switch():
# switch the pageelse:
# don't switch the page
Then, it's just a matter of implementing ok_to_switch
in every page class.
Post a Comment for "Function To Switch Between Two Frames In Tkinter"