使用uv运行Tkinter程序出现xcb错误,如何解决?
解决uv环境下Tkinter运行XCB断言错误的问题
问题重现
运行如下Tkinter脚本(xxx.py):
import tkinter as tk def main(): root = tk.Tk() label = tk.Label(root, text="This line causes the problem") label.pack() root.mainloop() if __name__ == "__main__": main()
执行命令:
uv run xxx.py
触发错误:
Using CPython 3.13.6 Creating virtual environment at: .venv [xcb] Unknown sequence number while appending request [xcb] You called XInitThreads, this is not your fault [xcb] Aborting, sorry about that. python: xcb_io.c:157: append_pending_request: Assertion `!xcb_xlib_unknown_seq_number' failed.
系统环境:
- Manjaro Linux,KDE Plasma 6.3.6,X11图形平台
- NVIDIA GeForce RTX 4060 Ti显卡
- Python 3.13.6(uv创建的虚拟环境)
解决方案
1. 禁用XCB线程冲突
在运行脚本前设置环境变量,阻止XCB与线程初始化的冲突:
export XCB_XLIB_NO_THREADS=1 uv run xxx.py
或者直接在脚本开头添加环境变量配置:
import os os.environ['XCB_XLIB_NO_THREADS'] = '1' import tkinter as tk def main(): root = tk.Tk() label = tk.Label(root, text="This line causes the problem") label.pack() root.mainloop() if __name__ == "__main__": main()
2. 降级Python版本
Python 3.13对Tkinter的XCB兼容性可能存在问题,尝试切换到3.12版本:
# 安装Python 3.12 uv python install 3.12 # 使用3.12运行脚本 uv run --python 3.12 xxx.py
3. 排除虚拟环境问题
直接用系统Python运行脚本,验证是否是uv虚拟环境导致的问题:
python xxx.py
如果系统Python能正常运行,尝试删除现有虚拟环境后重新创建:
rm -rf .venv uv run xxx.py
4. 更新NVIDIA驱动
确保NVIDIA显卡驱动是最新兼容版本,在Manjaro中执行:
sudo mhwd -a pci nonfree 0300
更新后重启系统再测试。
内容的提问来源于stack exchange,提问作者Psionman
相关产品推荐
相关产品推荐

