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 commands below (as he did):
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))`
I'd like to be able to create a function that had an if/then hierarchy inside of it... specifically to check if all other inputs on PageOne had been fulfilled first (which I then know how to do) before allowing a frame change.
If this can be done individually using lambda, even better. Can anyone help me out?
Update: Using Bryan's advice and reformatting for the original code he linked, I now have:
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "APP") #window heading
self.title_font = tkfont.Font(family='Helvetica', size=12) #options: weight="bold",slant="italic"
container = tk.Frame(self) #container = stack of frames; one on top is visible
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 (StartPage, PageOne):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame #puts all pages in stacked order
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name): #show a frame for the given page name
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is the start page", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame("PageOne"))
button1.pack()
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
####FIX PART 1####
self.next1 = tk.Button(self,text="Next",padx=18,highlightbackground="black",
command=lambda: self.maybe_switch("PageTwo"))
self.next1.grid(row=10,column=1,sticky='E')
####FIX PART 2####
def maybe_switch(self, page_name):
if ###SOMETHING###:
self.controller.show_frame(page_name)
if __name__ == "__main__":
app = App()
app.mainloop()