Guest User

`.tksleep` for those who dislike `.after`

a guest
Nov 16th, 2022
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | Source Code | 0 0
  1. # Partially copied from https://stackoverflow.com/a/74162322/11106801
  2. import tkinter as tk
  3.  
  4. def tksleep(self, time:float) -> None:
  5.     """
  6.    Emulating `time.sleep(seconds)`
  7.    Created by TheLizzard, inspired by Thingamabobs
  8.    """
  9.     self.after(int(time*1000), self.quit)
  10.     self.mainloop()
  11.  
  12. tk.Misc.tksleep = tksleep # Monkey patching
  13.  
  14.  
  15. # Test code
  16. root = tk.Tk()
  17. for i in range(30):
  18.     root.tksleep(1)
  19.     print(i)
  20. root.mainloop()
Tags: python tkinter
Advertisement
Add Comment
Please, Sign In to add comment