You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter文本框内容替换无效问题求助

解决Tkinter中禁用Text组件无法更新的问题

你的代码中,e1和e2这两个Text组件被设置为disabled状态,Tkinter会阻止任何对禁用组件的编辑操作——包括通过代码调用delete和insert方法,这就是它们无法更新的核心原因。

修改方案

在更新e1和e2的内容前,先将它们的状态临时切换为normal,完成修改后再恢复为disabled即可。

修改后的work_change函数

def work_change(event):
    with open("uld.json", 'r', encoding="utf-8") as f:
        data = json.load(f)
        # 去除Text组件自动添加的换行符
        code_input = e3.get("1.0", "end-1c").strip() 
    
        if not code_input:  # 空输入直接返回
            return
        
        try:
            code_input = int(code_input)
            for i in range(len(data)):
                if data[i]["code"] == code_input or data[i]["code1"] == code_input:
                    description = data[i]["description"]
                    typeof = data[i]["type"]
                
                    # 临时启用e1和e2以允许修改
                    e1.configure(state="normal")
                    e2.configure(state="normal")
                    
                    e1.delete("1.0", "end")
                    e1.insert('end', description)
                    e2.delete("1.0", "end")
                    e2.insert('end', typeof)
                    
                    # 恢复禁用状态
                    e1.configure(state="disabled")
                    e2.configure(state="disabled")
                    
                    break
        except ValueError:
            pass  # 处理非整数输入的情况

补充说明

  • 你在insert_labour函数中先插入内容再设置禁用的逻辑是正确的,无需修改
  • 确保uld.json中的code和code1字段为整数类型,避免类型转换报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 02:24:55