Python Tkinter线程 - 当用户关闭GUI

时间:2018-02-04 18:01:54

标签: python multithreading tkinter

如果我有一个非for循环线程任务与tkinter一起运行,例如time.sleep(seconds)我怎么能在结束之前安全地结束任务,或者在time.sleep()结束之前。

如果您运行程序并单击按钮然后关闭GUI窗口,那么几秒钟后finished仍将在解释器窗口中打印。

请注意,在这种情况下,time.sleep()只是长期运行非for循环函数的占位符。

# python 3 imports
import tkinter as tk
import threading
import queue
import time

def center_app(toplevel):
    toplevel.update_idletasks()
    w = toplevel.winfo_screenwidth() - 20
    h = toplevel.winfo_screenheight() - 100
    size = tuple(int(Q) for Q in toplevel.geometry().split("+")[0].split("x"))
    toplevel.geometry("%dx%d+%d+%d" % (size + (w / 2 - size[0] / 2, h / 2 - size[1] / 2)))

class app(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.protocol("WM_DELETE_WINDOW", self.USER_HAS_CLOSED_WINDOW)
        self.b1 = tk.Button(self, text = "*** Start Thread ***", font = ("Arial",25,"bold"), command = self.func_1)
        self.b1.pack(side = "top", fill = "both", expand = True)
        center_app(self)
        self.queue = queue.Queue()

    def USER_HAS_CLOSED_WINDOW(self, callback = None):
        print ("attempting to end thread")
        # end the running thread
        self.destroy()

    def func_1(self):
        self.b1.config(text = " Thread Running ")
        self.b1.update_idletasks()
        t = ThreadedFunction(self.queue).start()
        self.after(50, self.check_queue)

    def check_queue(self):
        try:
            x = self.queue.get(0) # check if threaded function is done
            self.b1.config(text = "*** Start Thread ***")
            self.b1.update_idletasks()
        except:
            self.after(50, self.check_queue)

class ThreadedFunction(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self, daemon = True)
        self.queue = queue
    def run(self):
        time.sleep(10) # how to end this if user closes GUI
        print ("finished")
        self.queue.put("result")

a = app()
a.mainloop()

我已经创建了一个函数USER_HAS_CLOSED_WINDOW来捕获关闭窗口的用户,在那里我想我会用一些东西来结束正在运行的线程,但我不确定如何。

我想另一种选择是在线程上放置一个合适的timeout并以某种方式使用.join()。但我不知道该怎么做

1 个答案:

答案 0 :(得分:1)

当您使用要中断的线程时,似乎应该使用Event对象并使用wait()方法而不是time.sleep

这可能会对你有所帮助:

  1. https://stackoverflow.com/a/5205180
  2. https://stackoverflow.com/a/38828735