本文介绍了Python Tkinter线程-用户关闭GUI时如何结束/杀死/停止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个非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().但是我也不知道该怎么做

解决方案

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

这可能会帮助您:

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

If I have a non for loop threaded task being run alongside tkinter such as time.sleep(seconds) how can I safely end the task before it has concluded, or, before the time.sleep() has ended.

If you run the program and click the button and then close the GUI window then finished will still print in the interpreter window a few seconds later.

Please note that in this case time.sleep() is just a placeholder for a long running non for loop function.

# 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()

I have made a function USER_HAS_CLOSED_WINDOW to catch the user closing the window, where I was thinking I would put something to end the running thread, but I'm not sure how.

I guess the other option is to put an appropriate timeout on the thread and use .join() somehow. But I'm not sure how to do that either

解决方案

It appears that you should use Event object and use wait() method and not time.sleep when you use thread that you want to interrupt.

Probably this can help you:

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

这篇关于Python Tkinter线程-用户关闭GUI时如何结束/杀死/停止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:13