python - Sleep() stops threads -
i trying run script spawns couple of threads using root.after(). sleep() seems lock script. never sees flag being set second root.after().
from time import sleep tkinter import * global flag flag = false def settimer(): global flag while(flag==false): sleep(.1) print('flag set') return def setflag(): global flag flag=true return root=tk() print('start') root.after(1,settimer()) root.after(100,setflag()) print('done')
after
not start new thread, runs method inside event-loop of tk. while-loop block program.
if use after
method must non-blocking operations, checking flag. if have background processes, use real threads. notice: must not alter gui inside these threads!
import tkinter tk time import sleep threading import thread, event flag = event() def worker(): while not flag.is_set(): sleep(.1) print('flag set') def set_flag(): flag.set() root = tk.tk() print('start') thread(target=worker).start() root.after(100, set_flag) print('done') root.mainloop()
Comments
Post a Comment