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

Python运行Tkinter代码报TypeError: 'Label' object is not callable如何解决

错误原因
  • 核心触发原因是变量名覆盖了tkinter官方的Label类:你在主窗口初始化代码中写了Label = Label(root, text="Are you ready?"),这行代码把原本从tkinter导入的Label类对象,直接替换成了你自己创建的Label实例对象。后续在open_window函数中调用Label(menu, ...)时,实际是尝试调用这个实例对象,而类实例本身不是可调用的函数/类,因此抛出TypeError: 'Label' object is not callable报错。
  • 次要问题:pack是tkinter的布局方法,必须加括号执行调用,你写的.pack只是引用了方法对象,不会实际执行布局操作。
  • 冗余逻辑问题:Toplevel子窗口不需要单独执行mainloop,全局只需要保留root窗口的mainloop即可,多余的menu.mainloop()可能导致后续窗口交互逻辑异常。
修复步骤
  1. 将主窗口中自定义Label实例的变量名,修改为不与官方类名冲突的名称,比如root_label
  2. 所有pack方法调用补充括号
  3. 删除open_window函数中的menu.mainloop()代码
修复后完整代码
from PIL import Image, ImageTk
from tkinter import *
from tkinter import Label

def open_window():
    menu = Toplevel(root)
    menu.geometry("800x800")
    menu.title("my game's menu")
    menu.resizable(False, False)
    lbl = Label(menu, text ="Hello!").pack()


root = Tk()
root.geometry("400x300")
root_label = Label(root, text="Are you ready?")
root_label.pack()


root.title("quick question")


btn = Button(root, text="Yes", command= open_window)
btn.pack(padx=20, pady = 20)
root.mainloop()

内容的提问来源于stack exchange,提问作者Btv beautiful music videos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 11:57:03