作者weel99699 (凉面)
看板Python
标题Re: [问题] 如何立即停止执行绪
时间Sun Aug 16 16:36:46 2020
※ 引述《cjs0710 (blue)》之铭言:
: 本身还是新手, 我用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放进下面的for loop里面
: if stop_var == 1:
: break
: for i in range(10):
#if stop_var == 1:
#. Break
: 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()
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 110.50.132.13 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1597567008.A.385.html