I am new to tkinter. I wanted to create an app to add two numbers and pass the result to the next pages. after working on the code and looking into the other posts, I got to this code to do that.
import tkinter as tk
class Data:
def __init__(self):
self.first_no = tk.IntVar()
self.second_no = tk.IntVar()
self.summ = 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")
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)
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()
data.summ.trace('w', lambda a, b, c: self.correct_label())
def correct_label(self):
self.label3.config(text=self.data.summ.get())
app = SampleApp()
app.mainloop()
Now, I want to extend this code to import a textfile that contains a few numbers, read that and transfer those numbers as a list to the next page. suppose we have a textfile with the name of Test.txt
which contains a few numbers. in the below code, the results show on the first page. Idon't know how to make connection between my opentext()
and new_Var
attribute.
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()
CodePudding user response:
You can update Data.new_Var
inside opentext()
and link this variable to PageTwo.label6
using textvariable
option:
class PageOne(tk.Frame):
def __init__(self, parent, data):
super().__init__(parent)
self.data = data
...
def opentext():
# why do you use askopenfilenames() instead of askopenfilename()???
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)
self.data.new_Var.set(lines) # update Data.new_Var
return lines
...
class PageTwo(tk.Frame):
def __init__(self, parent, data):
super().__init__(parent)
self.data = data
...
self.label6 = tk.Label(self.frame2, textvariable=self.data.new_Var)
self.label6.pack()
...
Note that you don't need to update self.label6
inside correct_label()
.