调试TKINTER代码给出“ RuntimeError:主线程不在主循环中”。

发布于 2025-02-06 20:41:25 字数 2116 浏览 0 评论 0原文

我知道互联网上充满了有关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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2025-02-13 20:41:26

RuntimeError:主线程不在主循环中
tcl_asyncdelete:异步处理程序被错误的线程删除

1.您可以将线程设置为守护程序:

t = threading.Thread(target=your_func)
t.setDaemon(True)
t.start()

2。实际上有一个tkinter的线程安全替代方案, mttkinter

RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread

1.You can set the thread to a Daemon:

t = threading.Thread(target=your_func)
t.setDaemon(True)
t.start()

2.There actually is a thread-safe alternative to Tkinter, mtTkinter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文