问题描述
限时送ChatGPT账号..在我的多线程 (*) 控制台应用程序中使用 tk 会导致它在没有堆栈跟踪的情况下崩溃,并给出消息Tcl_WaitForEvent: Notifier not initialized Abort trap".
Using tk in my multi-threaded(*) console application causes it to crash without stacktrace, giving the message "Tcl_WaitForEvent: Notifier not initialized Abort trap".
症状是我的程序的所有功能都运行良好,直到我调出 tk 窗口 - 然后接下来的操作会导致崩溃.
The symptoms were that all my program's functions worked fine, until I brought up the tk window - then the very next operation would cause the crash.
立即搜索发现 Tkinter 对于 Python 线程并不安全,因此我确保我没有在主线程以外的任何地方调用任何 Tk 函数.崩溃仍在继续.
Immediate searching found that Tkinter is not safe with respect to Python threads, so I made sure that I was not calling any Tk functions anywhere other than my main thread. The crashes continued.
我损失了几个小时,因为我认为是我使用的特定命令导致程序崩溃 - 但最终我意识到任何键盘输入都会导致程序崩溃.
I lost several hours because I believed that it was the specific command I was using that crashed the program - but eventually I realized that any keyboard input would crash the program.
经过大量调试后,我最终将其归结为一个小程序来演示该问题,暴露出我认为是错误或肯定需要 Tkinter 库中文档的功能.
After a lot of debugging, I finally boiled it down to a small program that demonstrates the issue, exposing what I believe is a bug or certainly a feature that needs documentation in the Tkinter library.
我正在处理这篇我正在调试的帖子.我将发布它并回答我自己的问题,希望它能防止下一个人在上面浪费一天的时间.
I was working on this posting I was debugging. I'm going to post it and answer my own question in the hopes that it will prevent the next person from wasting a day on it.
--
(* - 是的,它当然需要是多线程的.我有一个用于套接字连接的线程,一个监听麦克风并查找电平的线程,一个驱动我的串行端口的线程等等.在每种情况下,我在线程上阅读的东西大部分时间自然会阻塞.)
(* - Yes, it certainly needs to be multi-threaded. I have a thread for my socket connection, a thread that listens to a mic and finds levels, a thread to drive my serial port and more. In each case, the thing I'm reading on the thread naturally blocks most of the time.)
推荐答案
就我而言(如@Tom Swirly 回答下的评论中所述),解决方案是切换到非交互式后端:
In my case (as mentioned in the comments under @Tom Swirly's answer) the solution was to switch to a non-interactive backend:
import matplotlib
matplotlib.use('Agg')
这篇关于使用 tk 的多线程 Python 控制台应用程序中模糊的、可重复的崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论