tkinter_eventloop.py 880 B

12345678910111213141516171819202122
  1. from statecharts_core import EventLoop
  2. import math
  3. class TkEventLoop(EventLoop):
  4. def __init__(self, tk):
  5. tk.sccd_force_update = False
  6. # bind scheduler callback
  7. def schedule(callback, timeout):
  8. if timeout == 0:
  9. # tk considers updating the window an 'idle' task, only to be done if no events are scheduled for a while. But this has the downside of the interface becoming completely unresponsive while the model is performing steps with no gaps in between. Thus we insert an 'update_idletasks()' to obtain "javascript event loop"-like behavior.
  10. if tk.sccd_force_update:
  11. tk.update_idletasks()
  12. tk.sccd_force_update = False
  13. else:
  14. return (False, None) # don't schedule 0-timeout, it's more performant to just keep running
  15. return (True, tk.after(int(math.ceil(timeout*1000.0)), callback))
  16. EventLoop.__init__(self, schedule, tk.after_cancel)