【发布时间】:2020-11-02 20:04:33
【问题描述】:
我是 Python 的初学者。 我使用 Tkinter 进行编码。我想用 "threading" 扩展来改进我的程序。 然而,在我的一些程序中,如果我离开 Tkinter 窗口,程序仍在运行。 我想知道如何阻止它。我猜这是仍在运行的线程。我尝试了不同的方法来关闭线程,但我发现的唯一问题是创建一个错误(在我看来这不是一个好的解决方案):)。
我想知道是否存在类似以下的指令来停止线程。
Thread(target=FinTemps).stop()
(我知道“停止”不起作用)
如果可以使用循环“while” 来停止线程,我试过了,但我不明白为什么它不起作用:
import tkinter as tk
from threading import Thread as Th
from time import *
global end
end=False
begining=time()
def screen():
global end
while end==False:
chrono=time()-beginning
if chrono<5:
sentence.set("Hey !")
elif chrono<8:
sentence.set("What's up ?")
window=tk.Tk()
window.geometry("{}x{}".format(fenêtre.winfo_screenwidth(), window.winfo_screenheight()))
window.title("Test Tkinter & Thread")
sentence=tk.StringVar()
sentence.set("none")
GreatTitle=tk.Label(window, textvariable=sentence, width=500)
Th(target=screen).start()
GreatTitle.pack(expand=1)
window.mainloop()
end=True
谢谢你;) (如果我的英语不是很好(我是法国人),或者我的代码或解释不是很容易理解或者没有遵循传统的介绍,很抱歉)。
祝你有美好的一天:)
【问题讨论】:
-
这能回答你的问题吗? How to stop a looping thread in Python?
-
@funie200:对于 OP 的要求,是的,它是重复的。但这绝对是an XY problem,而且解决方案与停止线程无关,所以我不会认为它是重复的。
标签: python multithreading tkinter