Cannot close multithreaded Tkinter app on X button

岁酱吖の 提交于 2019-12-02 12:02:13

Main event loop should in main thread, and the drawing thread should in the second thread.

The right way to write this app is like this:

import tkinter as tk
from threading import Thread

class DrawingThread(Thread):
    def __init__(wnd):
        self.wnd = wnd
        self.is_quit = False

    def run():
        while not self.is_quit:
            ... # drawing sth on window

    def stop():
        # to let this thread quit.
        self.is_quit = True

class MyWindow(tk.Frame):
    ...  # constructor, methods etc.
    self.thread = DrawingThread(self)
    self.thread.start()

    on_close(self, event):
        # stop the drawing thread.
        self.thread.stop()

def main():
    window = MyWindow()
    window.mainloop()

main()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!