我如何从 tkinter 中的条目获取字符串?我使用 .get(),但它只有 returns 0

How do i get string from Entry in tkinter? I use .get(), but it only returns 0

我无法从 tkinter 中的 Entry 返回结果。创建条目后我没有立即使用它:

import tkinter as tk

window = tk.Tk()
window.title('Enchantment generator')
window.geometry('800x600+400+300')
window.resizable(False, False)
window.entrylist = []
window.cbuttlist = []

res_lvl_mas = []
res_mas = []

def enchwind(chk_ench, ench, txt_ench, lvl_ench, ench_name, row, column):
    chk_ench = tk.BooleanVar()
    chk_ench.set(False)
    ench = tk.Checkbutton(window, text=ench_name, var=chk_ench)
    ench.grid(row=row, column=column, sticky='w')
    txt_ench = tk.Entry(window, width=3)
    txt_ench.grid(row=row, column=column+1)
    window.entrylist.append(txt_ench)
    window.cbuttlist.append(chk_ench)

def resstr(lvl, resmas):
    res = ''
    list = ['/give @p ', '{', 'Enchantments:[']
    for i in range(len(lvl)):
        list.append('{id:"minecraft:')
        list.append(resmas[i])
        list.append('",lvl:')
        list.append(lvl[i])
        if i != len(lvl) - 1:
            list.append('}, ')
        else:
            list.append('}')
    list.append(']}')
    for i in range(len(list)):
        res += list[i]
    return res

ench_mas = []
name_mas = ['Aqua affinity', 'Bane of arthropods', 'Binding curse', 'Blast protection',
            'Channeling', 'Depth strider', 'Efficiency', 'Feather falling', 'Fire aspect',
            'Fire protection', 'Flame', 'Fortune', 'Frost walker', 'Impaling', 'Infinity',
            'Knockback', 'Looting', 'Loyalty', 'Luck of the sea', 'Lure', 'Mending',
            'Power', 'Projective protection', 'Protection', 'Punch', 'Respiration',
            'Riptide', 'Sharpness', 'Silk touch', 'Smite', 'Sweeping', 'Thorns',
            'Unbreaking', 'Vanishing curse']

for i in range(34):
    ench_mas.append([])
for i in range(34):
    for j in range(7):
        ench_mas[i].append(0)
for i in range(34):
    ench_mas[i][4] = name_mas[i]
for i in range(17):
    ench_mas[i][5] = i+1
    ench_mas[i][6] = 0
for i in range(17, 34):
    ench_mas[i][5] = i-16
    ench_mas[i][6] = 2

ench_list = tk.Label(window, text='Choose the enchantments:')
ench_list.grid(row=0, column=0, columnspan=4)

result = tk.Label(window, text='None')
result.grid(row=19, column=0, columnspan=4)

for i in range(34):
    enchwind(ench_mas[i][0], ench_mas[i][1], ench_mas[i][2], ench_mas[i][3], ench_mas[i][4], ench_mas[i][5], ench_mas[i][6])

def clicked():
    result.configure(text=txt)

for i in range(34):
    if window.cbuttlist[i].get():
        res_lvl_mas.append(window.entrylist[i].get())
        res_mas.append(ench_mas[i][4])

txt = resstr(res_lvl_mas, res_mas)

btn = tk.Button(window, text='Generate!', command=clicked)
btn.grid(column=0, row=18, columnspan=4)

window.mainloop()

此代码创建用于附魔我的世界物品的命令。如您所见,我有 34 个条目,所以我决定将它们的参数存储为列表列表。在程序的最后,当我想获取条目中的数字时,它总是 returns 0。界面看起来像这样:Interface。 我想这些可能是代码第一部分留下的零,只是为了定义列表参数。 我应该移动代码中的某些内容还是根本不正确?

问题在于您不记得条目小部件。您只需将它们存储到函数 enchwind() 中的一个局部变量中,不要将它们存储在任何地方。它们已创建,但您将失去所有参考。

您可以使用列表作为 window 的属性来存储它们:

在您的 window 的定义中,添加行:

window.entrylist = []

然后,在我们的 enchwind() 函数中,您可以将条目存储到此列表中:

    txt_ench = tk.Entry(window, width=3)
    txt_ench.grid(row=row, column=column+1)
    window.entrylist.append(txt_ench)

现在,您有一个包含 34 个项目的列表,其中包含所有文本条目。您可以通过获取此列表的其中一个元素的内容来访问它们(即 value = window.entrylist[5].get() 将获取第 6 个条目小部件的内容。