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

2024-04-27 01:09:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在返回tkinter中输入的结果时遇到问题。我不会在创建条目后立即使用它:

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个入口,所以我决定将它们的参数存储为列表列表。在程序的最后,当我想得到entrys中的数字时,它总是返回0。接口看起来像:Interface。 我想这些可能是代码第一部分留下的零,只是为了定义列表参数。 我应该在代码中移动一些东西还是根本不正确


1条回答
网友
1楼 · 发布于 2024-04-27 01:09:42

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

您可以使用列表作为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个条目小部件的内容

相关问题 更多 >