Feb-14-2022, 05:37 AM
You don't need a lambda expression for something like this:
In this code you are creating a string by concatenating the strings in entry1 and entry2.
command=lambda: self.go_to_page_two()Instead use:
command=self.go_go_page_twoOnly use a lambda if you want to specify arguments for the method call.
In this code you are creating a string by concatenating the strings in entry1 and entry2.
self.controller.SomeVar = self.entry1.get() + self.entry2.get()I do not like Page1 having to know about things in Page2. For something small like this it is ok, but if you had several windows that share information, this way of keeping everything current quickly becomes unmaintainable. A better design splits out all the information you are working with into it's own entity. Frame 1 would change a value in this data entity. If Frame 2 has a display for that data, registers a function to call when the value of the data changes.
import tkinter as tk class Data: """I an a data entity shared by the forms""" def __init__(self): self.first_no = tk.IntVar() self.second_no = tk.IntVar() self.someVar = tk.IntVar() class SampleApp(tk.Tk): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.title("Summation App") container = tk.Frame(self) container.pack() self.data = Data() self.frames = {} for F in (PageOne, PageTwo): frame = F(container, self.data) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") # I knob about all the frames, so it is ok for me to reference parts of their API self.frames[PageOne].page_button.config(command=self.go_to_page_two) self.show_frame(PageOne) def go_to_page_two(self): # I do not update the the label. There are limits self.data.someVar.set(self.data.first_no.get() + self.data.second_no.get()) self.show_frame(PageTwo) def show_frame(self, c): frame = self.frames[c] frame.tkraise() class PageOne(tk.Frame): def __init__(self, parent, data): super().__init__(parent) self.data = data self.frame1 = tk.LabelFrame(self, text="This is page one") self.frame1.pack(padx=10, pady=10) label1 = tk.Label(self.frame1, text= "First No.") label1.grid(row=0, column=0) label2 = tk.Label(self.frame1, text= "Second No.") label2.grid(row=1, column=0) self.entry1 = tk.Entry(self.frame1, textvariable=data.first_no) self.entry1.grid(row=0, column=1) self.entry2 = tk.Entry(self.frame1, textvariable=data.second_no) self.entry2.grid(row=1, column=1) self.page_button = tk.Button(self.frame1, text="Go to Page Two") self.page_button.grid(row=4, column=0, padx=10, pady=10) class PageTwo(tk.Frame): def __init__(self, parent, data): super().__init__(parent) self.data = data self.frame2 = tk.LabelFrame(self, text="This is page two") self.frame2.pack() self.label3 = tk.Label(self.frame2) self.label3.pack() # When someone changes someVar I need to update the label data.someVar.trace('w', lambda a, b, c: self.correct_label()) def correct_label(self): self.label3.config(text=self.data.someVar.get()) app = SampleApp() app.mainloop()