【发布时间】: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