我使用那里學到的基礎撰寫了一個應用程式,該應用程式讀取一個文本檔案并洗掉冗余行,并回傳一個數字串列。下面的代碼回傳 中的數字串列PageOne,但由于我想在 中使用該串列PageTwo,它會更改為字串。我知道我最初將變數作為字串引入,但它在其他型別下也不起作用。我怎樣才能將其保留為串列?要運行此代碼,您可以在文本檔案中放置幾個??空格分隔的數字,然后使用應用程式打開它。
import tkinter as tk
from tkinter import filedialog
class Data:
def __init__(self):
self.Tot_grids = tk.IntVar()
self.NO = tk.StringVar()
self.NEW = tk.StringVar()
self.NEW2 = tk.StringVar()
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("500x200")
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].button2.config(command=self.go_to_page_two)
self.show_frame(PageOne)
def go_to_page_two(self):
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
def opentext():
my_file = filedialog.askopenfilenames(initialdir="/pycharm", title="Select your file")
for T in my_file: # this line should be here when opening multiple files
import re
with open(T, 'r') as infile1:
lines = infile1.read()
nums = []
for n in re.findall(r'(\d \.\d |\d \*\d )', lines):
split_by_ast = n.split("*")
if len(split_by_ast) == 1:
nums = [float(split_by_ast[0])]
else:
nums = [float(split_by_ast[1])] * int(split_by_ast[0])
nums = nums[1:len(nums)]
data.NO.set(nums)
label3.config(text=nums)
label4.config(text=type(nums))
self.button1 = tk.Button(self, text="Open file(s)", command=opentext)
self.button1.grid(row=0, column=0, columnspan=2, pady=20)
label1 = tk.Label(self, text="imported numbers:")
label1.grid(row=1, column=0, pady=10)
label2 = tk.Label(self, text="type of imported numbers:")
label2.grid(row=2, column=0, pady=5)
label3 = tk.Label(self)
label3.grid(row=1, column=1)
label4 = tk.Label(self)
label4.grid(row=2, column=1, pady=5)
self.button2 = tk.Button(self, text="Continue")
self.button2.grid(row=3, column=0, columnspan=2, pady=10)
class PageTwo(tk.Frame):
def __init__(self, parent, data):
super().__init__(parent)
self.data = data
self.label5 = tk.Label(self, textvariable=self.data.NEW)
self.label5.pack()
self.label5 = tk.Label(self, textvariable=self.data.NEW2)
self.label5.pack()
self.data.NO.trace_add('write', self.on_grids_updated)
def on_grids_updated(self, *args):
self.data.NEW.set(self.data.NO.get())
self.data.NEW2.set(type(self.data.NO.get()))
app = SampleApp()
app.mainloop()
我還想知道為什么 PageOne 中的串列型別會顯示一些亂數?謝謝
uj5u.com熱心網友回復:
您可以使用Variablewhich 是 , 等的基StringVar類IntVar。
Variable.get()不會像StringVar.get()或那樣轉換存盤的資料IntVar.get()。但是,似乎將由 TCL 內部list轉換為。tuple
class Data:
def __init__(self):
self.Tot_grids = tk.IntVar()
self.NO = tk.Variable() # used Variable instead of StringVar
self.NEW = tk.StringVar()
self.NEW2 = tk.StringVar()
還要換行
label4.config(text=type(nums))
到
label4.config(text=str(type(nums)))
將正確顯示型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/427102.html
上一篇:tkinter如何將小部件向右對齊,而與左側小部件的長度無關?
下一篇:如何自行呼叫tkinter按鈕?
