You need to enable JavaScript to run this app. tkinter调用Entry的.get()方法仅返回0的问题该如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

tkinter调用Entry的.get()方法仅返回0的问题该如何解决?

问题原因
  • 核心逻辑错位:读取输入框数值、收集选中附魔、生成指令的代码写在了按钮点击回调的外部,程序启动时就会执行一次,此时你还没有进行任何输入或勾选操作,拿到的永远是初始状态的空值。点击按钮时只是把早就生成好的错误结果展示出来,不会重新读取最新的输入内容。
  • 指令格式错误:直接使用附魔显示名(如Aqua affinity)拼接指令,Minecraft可识别的附魔ID为小写加下划线格式(如aqua_affinity),原写法会导致生成的指令无效。
  • 转义字符错误:代码中的"是HTML转义字符,Python语法中直接使用双引号即可。
修正方案

将收集输入、生成指令的逻辑全部移入clicked函数内部,每次点击按钮都会重新读取当前的勾选和输入状态,生成对应指令。

修正后完整代码
import tkinter as tk

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

def enchwind(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 = ''
    cmd_list = ['/give @p minecraft:diamond_sword ', '{', 'Enchantments:[']
    for i in range(len(lvl)):
        cmd_list.append('{id:"minecraft:')
        cmd_list.append(resmas[i])
        cmd_list.append('",lvl:')
        cmd_list.append(lvl[i])
        if i != len(lvl) - 1:
            cmd_list.append('}, ')
        else:
            cmd_list.append('}')
    cmd_list.append(']}')
    for part in cmd_list:
        res += part
    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][4], ench_mas[i][5], ench_mas[i][6])

def clicked():
    res_lvl_mas = []
    res_mas = []
    for i in range(34):
        if window.cbuttlist[i].get():
            lvl = window.entrylist[i].get().strip()
            # 未填写等级默认取1
            if not lvl:
                lvl = '1'
            res_lvl_mas.append(lvl)
            # 转换为Minecraft可识别的附魔ID格式
            ench_id = ench_mas[i][4].lower().replace(' ', '_')
            res_mas.append(ench_id)
    txt = resstr(res_lvl_mas, res_mas)
    result.configure(text=txt)

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

window.mainloop()

注:示例代码默认生成钻石剑的附魔指令,可自行修改resstr函数内的物品ID适配需求。

内容的提问来源于stack exchange,提问作者Ambassador4ik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 13:06:05