Tkinter Button组件报错:预期整数却获字符串'Nerd',如何解决?
问题描述
在Python中使用Tkinter创建Button组件时,窗口能正常显示,但按钮创建失败,报错信息如下:
Traceback (most recent call last): File "/home/saif/new/c.py", line 11, in <module> button = tk.Button(root, text="Click Me", command=button_clicked) File "/usr/lib/python3.10/tkinter/__init__.py", line 2679, in __init__ Widget.__init__(self, master, 'button', cnf, kw) File "/usr/lib/python3.10/tkinter/__init__.py", line 2601, in __init__ self.tk.call( _tkinter.TclError: expected integer but got "Nerd"
使用的源代码:
import tkinter as tk # 创建主窗口 root = tk.Tk() # 按钮点击触发的函数 def button_clicked(): print("Button clicked!") # 创建按钮组件 button = tk.Button(root, text="Click Me", command=button_clicked) button.pack() # 运行Tkinter事件循环 root.mainloop()
环境信息:
Python 3.10.12 tk 0.1.0 tk-tools 0.16.0 Linux Mint 21.1 (Xorg)
解决方案
报错的核心原因是你安装的第三方tk包(版本0.1.0)与Python标准库的tkinter冲突,该非官方包的实现存在问题,导致组件初始化时错误解析参数,触发"expected integer but got 'Nerd'"的错误。
解决步骤:
- 卸载第三方
tk包:
pip uninstall -y tk
- 系统自带的
tkinter属于Python标准库,无需额外安装,直接运行原代码即可正常创建按钮。
内容的提问来源于stack exchange,提问作者Saif Shahriar
相关产品推荐
相关产品推荐

