(Feb-14-2022, 08:17 PM)deanhystad Wrote: It is not good user interface design to have controls in one "page" change displays in a second "page". It is not an absolute, but should give you pause. If you don't immediately see the results of your actions, how do you know that you performed the action correctly? For your example I would put a button in frame 2 that draws a dialog to enter values for first_no and second_no which are applied when you press the "Ok" button in the dialog, or ignored if you press the "Cancel" button.
I cannot tell you what is a good design for your application (I don't know what you are trying to achieve), but I can say you should be spending most of your programming time thinking about the user experience and relatively little time writing code.
I am still working on this problem. I am so confused. could you please help me with that. imagine I have a text file with a few numbers in that, and I want to pass it as a string to the next page. I updated my code. I created a new attribute in Data class as a string (new_Var). I cannot figure out how to make the connection between my opentext() function and the new_Var.
import tkinter as tk from tkinter import filedialog class Data: def __init__(self): self.first_no = tk.IntVar() self.second_no = tk.IntVar() self.summ = tk.IntVar() self.new_Var = tk.StringVar() 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") self.frames[PageOne].page_button.config(command=self.go_to_page_two) self.show_frame(PageOne) def go_to_page_two(self): self.data.summ.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 frame1 = tk.LabelFrame(self, text="This is page one") frame1.pack(padx=10, pady=10) label1 = tk.Label(frame1, text="First No.") label1.grid(row=0, column=0) label2 = tk.Label(frame1, text="Second No.") label2.grid(row=1, column=0) self.entry1 = tk.Entry(frame1, textvariable=data.first_no) self.entry1.grid(row=0, column=1) self.entry2 = tk.Entry(frame1, textvariable=data.second_no) self.entry2.grid(row=1, column=1) self.page_button = tk.Button(frame1, text="Go to Page Two") self.page_button.grid(row=3, column=0, padx=10, pady=10) label3 = tk.Label(frame1, text="Import the file") label3.grid(row=2, column=0, padx=10) def opentext(): my_file = filedialog.askopenfilenames(initialdir="/pycharm", title="Select your file") for T in my_file: with open(T, 'r') as infile1: lines = infile1.read() label4.config(text=lines) return lines self.button1 = tk.Button(frame1, text="Open file(s)", command=opentext) self.button1.grid(row=2, column=1, padx=10, pady=10) label4 = tk.Label(frame1) label4.grid(row=3, column=1) 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.label5 = tk.Label(self.frame2) self.label5.pack() self.label6 = tk.Label(self.frame2) self.label6.pack() # When someone changes summ, I need to update the label data.summ.trace('w', lambda a, b, c: self.correct_label()) def correct_label(self): self.label5.config(text=self.data.summ.get()) self.label6.config(text=self.data.new_Var) app = SampleApp() app.mainloop()