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

创建tkinter.Toplevel()对象时出现'str' object not callable错误

解决Tkinter Toplevel创建时的TypeError: 'str' object is not callable问题

让我们一步步拆解你的问题:

核心错误原因

从报错栈能明确看到,Toplevel初始化时执行了self.title(root.title()),这里的root就是你传入的self.parent。正常情况下,tkinter窗口对象的title是一个可调用的方法,用来获取或设置窗口标题,但现在你的self.parent.title被错误地赋值成了字符串,所以调用root.title()时就触发了'str' object is not callable的错误。

修复步骤

1. 修复self.parent的title属性赋值问题

找到self.parent(也就是AdminApplication实例或者它的父窗口)中所有修改标题的代码,确保是用方法调用而不是直接赋值:

  • ❌ 错误写法(直接把title属性改成字符串):
    self.parent.title = "Admin Application"
    
  • ✅ 正确写法(调用title()方法设置标题):
    self.parent.title("Admin Application")
    

2. 顺便修复按钮command的常见bug

虽然这不是当前报错的原因,但你的弹窗按钮代码存在逻辑问题:现在的写法会在弹窗创建时就直接执行方法,而不是点击按钮时执行:

  • ❌ 错误写法:
    leave = tkinter.Button(confirmPopup, text="Leave", height=0, width=100, command=self.changeRootSettings(self.page))
    cancel = tkinter.Button(confirmPopup, text="Cancel", height=0, width=100, command=confirmPopup.destroy())
    
  • ✅ 正确写法(用lambda包装带参数的方法,不带参数的直接传方法对象):
    leave = tkinter.Button(confirmPopup, text="Leave", height=0, width=100, command=lambda: self.changeRootSettings(self.page))
    cancel = tkinter.Button(confirmPopup, text="Cancel", height=0, width=100, command=confirmPopup.destroy)
    

验证修复

完成上述修改后,重新运行代码,点击Home菜单时应该能正常弹出确认窗口,不会再触发TypeError。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:25:52