我有帶有一些輸入框的 Tkinter GUI,底部有一個按鈕,我想單擊該按鈕以從框中檢索值。
棘手的部分是,我的 GUI 中的輸入框數量不是固定的。
在我的代碼前面,我生成了一個串列,該串列中的專案數是我正在生成的輸入框的數量,使用以下內容:
from tkinter import *
...
root = Tk()
root.title('Title')
title_label = Label(root, text="Inputs", font=("Helvetica", 14)).grid(row=0, column=0, columnspan=2, pady="8")
idx = 0
for i in list:
globals()['label%s' % idx] = Label(root, text=i).grid(row=(idx 1), column=0, sticky=W, padx=12)
globals()['entry%s' % idx] = Entry(root, text=i).grid(row=(idx 1), column=1, sticky=W, padx=12)
idx = 1
upload_prices = Button(root, text="Upload Prices").grid(row=16, column=0, padx=10, pady=10)
root.mainloop()
但是現在我有點卡住了,因為我正在努力定義一個命令函式,該函式也是可變的,以便在我的每個“entry0,entry1,entry2”框上使用 .get() - 不管有多少。我努力了:
from tkinter import *
...
root = Tk()
root.title('Title')
title_label = Label(root, text="Inputs", font=("Helvetica", 14))
title_label.grid(row=0, column=0, columnspan=2, pady="8")
def upload_prices():
global alist
for i in range(len(alist)):
globals()['entry_get' str(i)] = globals()['entry%s' % i].get()
idx = 0
for i in alist:
globals()['label%s' % idx] = Label(root, text=i)
globals()['label%s' % idx].grid(row=(idx 1), column=0, sticky=W, padx=12)
globals()['entry%s' % idx] = Entry(root, text=i)
globals()['entry%s' % idx].grid(row=(idx 1), column=1, sticky=W, padx=12)
idx = 1
upload_prices_button = Button(root, text="Upload Prices", command=upload_prices)
upload_prices_button.grid(row=16, column=0, padx=10, pady=10)
root.mainloop()
任何幫助將不勝感激!
uj5u.com熱心網友回復:
由于您像這樣定義條目:
idx = 0
for i in list:
globals()['label%s' % idx] = Label(root, text=i).grid(row=(idx 1), column=0, sticky=W, padx=12)
globals()['entry%s' % idx] = Entry(root, text=i).grid(row=(idx 1), column=1, sticky=W, padx=12)
idx = 1
您需要以相同的方式獲取值:
for i in range(len(list)):
globals()["entry_get" str(i)] = globals()['entry%s' % i].get()
這會將條目 1 保存到entry_get1等等。
如果您想將它們保存到 abc 等,您可以執行以下操作:
idx = 0
for i in 'abcdefghijklmno':
globals()[i] = globals()['entry%s' % idx].get()
idx =1
您的函式可能如下所示:
def upload_prices():
global list
for i in range(len(list)):
globals()['entry_get' str(i)] = globals()['entry%s' % i].get()
我不建議使用list關鍵字作為變數。
此外,upload_prices是一個按鈕和功能,這將證明不好。像這樣改變一個或另一個:
upload_prices_button = Button(root, text="Upload Prices", command=upload_prices)
upload_prices_button.grid(row=16, column=0, padx=10, pady=10)
您也不能同時定義和網格化條目,所以這里是完整的作業代碼:
from tkinter import *
...
root = Tk()
root.title('Title')
title_label = Label(root, text="Inputs", font=("Helvetica", 14))
title_label.grid(row=0, column=0, columnspan=2, pady="8")
My_list = [i for i in range(10)]
def upload_prices():
for i in range(len(My_list)):
globals()['entry_get' str(i)] = globals()['entry ' str(i)].get()
idx = 0
for i in My_list:
globals()['label ' str(idx)] = Label(root, text=i)
globals()['label ' str(idx)].grid(row=(idx 1), column=0, sticky=W, padx=12)
globals()['entry ' str(idx)] = Entry(root, text=i)
globals()['entry ' str(idx)].grid(row=(idx 1), column=1, sticky=W, padx=12)
print(globals()['entry ' str(idx)])
idx = 1
upload_prices_button = Button(root, text="Upload Prices", command=upload_prices)
upload_prices_button.grid(row=16, column=0, padx=10, pady=10)
root.mainloop()
注意:我更改list為My_list.
另一點:我會推薦你??一本字典,而不是globals():
My_Dict["entry" str(i)] = Entry()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/417328.html
標籤:
