python3和tkinter,线程连接阻塞主线程

2024-10-01 05:06:49 发布

您现在位置:Python中文网/ 问答频道 /正文

所以基本上我有一个类,比如SerialClass,它创建一个本地线程来更新一个combobox,构造函数如下所示:

def __init__(self, master):

    ...
    # Local thread.
    self.__thread = threading.Thread(target=self.__thread_handler)
    self.__thread_finished = False
    self.__thread.start()

这是线程执行的函数,它只是用值更新组合框。在

^{pr2}$

这是我用来退出线程的函数:

def thread_exit(self):
    self.__thread_finished = True
    self.__thread.join()

另一个类,比如App,实例化了SerialClass类。App创建一个Tk对象并使用以下方法启动主应用程序:

def run(self):
    # Handle the close window event.
    self.__root.protocol("WM_DELETE_WINDOW", self.__on_close)
    # Start the application and wait for user events.
    self.__root.mainloop()

这是一种“打开关闭”方法:

def __on_close(self):
    # When the application finishes, handle the rest of the threads.
    self.__serialui.thread_exit()
    # Destroy the root window as expected.
    self.__root.destroy()

我遇到的问题是程序流从未到达根目录。销毁()函数,由于某些原因,当我调用thread_exit函数时,主线程永远被阻塞。如果我评论self.update_端口()在thread_handler函数中,所有进程都优雅地关闭。在

有什么想法吗?在


Tags: the方法函数selfappclosedefexit