我是 tkinter 的新手。我想創建一個應用程式來添加兩個數字并將結果傳遞到下一頁。在處理代碼并查看其他帖子之后,我得到了這個代碼來做到這一點。
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()
現在,我想擴展此代碼以匯入一個包含一些數字的文本檔案,讀取它并將這些數字作為串列傳輸到下一頁。假設我們有一個文本檔案,其名稱Test.txt包含幾個數字。在下面的代碼中,結果顯示在第一頁。我不知道如何在我的opentext()和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()
uj5u.com熱心網友回復:
您可以Data.new_Var在內部更新opentext()并將此變數鏈接到PageTwo.label6usingtextvariable選項:
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()
...
請注意,您不需要self.label6在correct_label().
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/425924.html
