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

计算器开发报错:'Button'对象不可调用,清除按钮Lambda函数异常

问题分析与解决

问题根源:变量名冲突

你定义了名为button_clear的函数,之后又用同一个名字创建了Button对象:

button_clear = Button(root, text="Clear", padx=25, pady=20, command=lambda: button_clear())

这行代码执行后,原本的button_clear函数被Button实例覆盖。当点击按钮触发lambda时,button_clear()实际是在调用这个Button对象,而Button实例本身不支持被调用,因此抛出TypeError: 'Button' object is not callable。

解决方案

有两种简单的修复方式,任选其一即可:

  1. 修改按钮变量名,避免和函数重名,比如改成btn_clear:

    # 保留原函数不变
    def button_clear():
        e.delete(0, END)
    
    # 按钮变量换名
    btn_clear = Button(root, text="Clear", padx=25, pady=20, command=button_clear)
    

    (这里不需要lambda,直接把函数名传给command就行,因为button_clear不需要参数)

  2. 修改清除函数名,比如改成clear_input:

    # 函数换名
    def clear_input():
        e.delete(0, END)
    
    # 按钮变量名不变
    button_clear = Button(root, text="Clear", padx=25, pady=20, command=clear_input)
    

内容的提问来源于stack exchange,提问作者Sumanth N S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 03:35:25