【问题标题】:How do i get string from Entry in tkinter? I use .get(), but it only returns 0如何从 tkinter 中的 Entry 获取字符串?我使用 .get(),但它只返回 0
【发布时间】:2021-05-22 14:47:35
【问题描述】:

我无法从 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 个条目,因此我决定将它们的参数存储为列表列表。在程序结束时,当我想获取条目中的数字时,它总是返回0。界面如下:Interface。 我想那些可能是代码第一部分留下的零,只是为了定义列表参数。 我应该在代码中移动一些东西还是根本不正确?

【问题讨论】:

    标签: python tkinter tkinter-entry


    【解决方案1】:

    问题在于您不记得 Entry 小部件。您只需将它们存储到函数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 个条目小部件的内容。

    【讨论】:

    • 谢谢!在那之后,我遇到了另一个问题 - 我需要程序仅将条目添加到最终字符串中,该条目位于检查按钮附近。我使用了那个结构:``` for i in range(34): if window.entrylist[i].get() is not None: res_lvl_mas.append(window.entrylist[i].get()) res_mas.append( ench_mas[i][4]) ``` 但它现在返回所有附魔,没有关卡。
    • 我已经解决了这个问题(你可以看到我是如何编辑代码的)但现在它又回到了根本不返回任何东西
    • 我测试了它,发现res_masres_lvl_mas 在我在 gui 中所做的一切都没有
    • 缩进你的for循环,它应该填充res_mas。目前,它只在启动时执行一次。当然,它什么也没做。缩进它,以便在您单击时执行它。 (并为每个i 插入调试打印,即print(window.cbuttlist[i].get()),这样您就可以看到复选框中的内容以及循环何时执行。
    • 现在可以了!还有一些问题,但我会处理的。谢谢!
    猜你喜欢
    • 2022-12-12
    • 2016-08-24
    • 2020-04-15
    • 2020-08-13
    • 2016-04-16
    • 1970-01-01
    • 2022-06-29
    • 2021-08-29
    • 2020-01-21
    相关资源
    最近更新 更多