检索 GUI 可变的 Tktinter 条目值

Retrieving Tktinter Entry Values where GUI is Variable

我有带有一些输入框的 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()

如有任何帮助,我们将不胜感激!

既然你这样定义条目:

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()