[问题] 如何立即停止执行绪

楼主: cjs0710 (blue)   2020-08-16 12:07:16
本身还是新手, 我用tkinter 做了两个按钮,Start和Stop
按了Start之后 会一直重复从0数到9,
按了Stop之后, 会印出stop 但不会马上停止, 需要完成一个loop之后才会停止
请问有什么方法可以马上停止
import tkinter as tk
import time
import threading
def k ():
global stop_var
stop_var = 0
while True:
if stop_var == 1:
break
for i in range(10):
print ( i)
time.sleep(0.5)
def run():
a = threading.Thread(target = k)
a.start()
def stop():
global stop_var
print ("stop")
stop_var = 1
window=tk.Tk()
buttonStart = tk.Button(window,width = 10, text = "Start", command = run)
buttonStop = tk.Button(window,width = 10, text = "Stop", command = stop)
buttonStart.pack()
buttonStop.pack()
window.mainloop()
作者: atrix (班班)   2020-08-16 16:15:00
多设几个检查点
作者: tttkkk (学到。)   2020-08-16 19:16:00
That means you need to re-write your code.Doing a loop in another loop won't solve your issue.
作者: Falldog (Yo)   2020-08-17 00:12:00
if stop_var == 1 搬到 for 里不就好了

Links booklink

Contact Us: admin [ a t ] ucptt.com