python结束任务之后如何关闭,Python Tkinter Threading-如何在用户关闭GUI时结束/杀死/停止线程...

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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值