调试TKINTER代码给出“ RuntimeError:主线程不在主循环中”。
我知道互联网上充满了有关tkinter提出的“ RuntimeError:主线程不在主循环中”的问题。但是几乎所有人都讨论了如何并行运行线程。 我没有(至少我想)。 我的问题是:代码运行完全很好的独立。但是,一旦在调试模式下显示GUI,直到发生此运行时错误之前,只是一个秒的问题。我最好的猜测是:调试器本身在并行线程中运行?!? 如果我是对的:我该怎么办?如何可靠地使用TKINTER的调试代码?
版本:
- Visual Studio代码1.68
- 3.9.13
最小示例:
import tkinter as tk
import tkinter.ttk as ttk
class GUI(ttk.Frame):
def __init__(self, master):
# Initialize.
super().__init__(master)
self.grid(row=0, column=0, sticky=tk.N + tk.E + tk.S + tk.W)
# Create radio buttons
self.button = ttk.Radiobutton(self, text='Yes', value=True)
self.button.grid(row=0, column=0, sticky=tk.E)
self.button2 = ttk.Radiobutton(self, text='No', value=False)
self.button2.grid(row=0, column=1, sticky=tk.E)
# Put an OK button to the bottom right corner.
self.w_ok = ttk.Button(self, text='OK', command=self._exit)
self.w_ok.grid(row=100, column=2, sticky=tk.SE)
def _exit(self):
self.master.destroy()
if __name__ == '__main__':
root = tk.Tk()
gui = GUI(root)
gui.mainloop()
根据 @Mingjie-Msft的要求,这里是启动。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"autoReload": {"enable": true}
}
]
}
Python
Exception ignored in: <function Variable.__del__ at 0x000001B5B67451F0>
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 363, in __del__
if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread
I know the internet is full of questions about Tkinter raising "RuntimeError: main thread is not in main loop". But almost all of them discuss about how to run threads in parallel.
I don't (at least I guess that).
My problem is: the code runs completely fine stand-alone. But as soon as a GUI is shown in debugging mode it's only a question of seconds until this Runtime Error occurs. My best guess: the debugger itself runs in a parallel thread?!?
If I am right: what can I do about it? How can I reliably debug code that uses TKinter?
Versions:
- Visual Studio Code 1.68
- Python 3.9.13
Minimal example:
import tkinter as tk
import tkinter.ttk as ttk
class GUI(ttk.Frame):
def __init__(self, master):
# Initialize.
super().__init__(master)
self.grid(row=0, column=0, sticky=tk.N + tk.E + tk.S + tk.W)
# Create radio buttons
self.button = ttk.Radiobutton(self, text='Yes', value=True)
self.button.grid(row=0, column=0, sticky=tk.E)
self.button2 = ttk.Radiobutton(self, text='No', value=False)
self.button2.grid(row=0, column=1, sticky=tk.E)
# Put an OK button to the bottom right corner.
self.w_ok = ttk.Button(self, text='OK', command=self._exit)
self.w_ok.grid(row=100, column=2, sticky=tk.SE)
def _exit(self):
self.master.destroy()
if __name__ == '__main__':
root = tk.Tk()
gui = GUI(root)
gui.mainloop()
On request of @MingJie-MSFT here is the launch.json of Vscode:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"autoReload": {"enable": true}
}
]
}
... and the error report:
Exception ignored in: <function Variable.__del__ at 0x000001B5B67451F0>
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 363, in __del__
if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.您可以将线程设置为守护程序:
2。实际上有一个tkinter的线程安全替代方案, mttkinter 。
1.You can set the thread to a Daemon:
2.There actually is a thread-safe alternative to Tkinter, mtTkinter.