看板 Python 關於我們 聯絡資訊
本身還是新手, 我用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() -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.216.174.14 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1597550838.A.815.html
atrix: 多設幾個檢查點 08/16 16:15
tttkkk: That means you need to re-write your code. 08/16 19:16
tttkkk: Doing a loop in another loop won't solve your issue. 08/16 19:17
Falldog: if stop_var == 1 搬到 for 裡不就好了 08/17 00:12