Advertisement
rs6000

Python GUI Countdown timer loop

Nov 6th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. #source https://bit.ly/2U0TT0J
  2. from tkinter import *
  3. root = Tk()
  4. root.geometry("150x120")
  5. canvas = Canvas(root)
  6. canvas.pack()
  7. def_time=10
  8. time = def_time
  9. def tick():
  10.     # You have to clear the canvas each time the clock updates
  11.     # (otherwise it writes on top of the old time).  Since the
  12.     # time is the only thing in the canvas, delete(ALL) works
  13.     # perfectly (if it wasn't however, you can delete the id
  14.     # that goes with the clock).
  15.     canvas.delete(ALL)
  16.     # I have to declare time as a global because I'm not using
  17.     # a class (otherwise, I could do something like self.time -= 1)
  18.     global time
  19.     time -= 1
  20.     # You can place the time wherever in the canvas
  21.     # (I chose 10,10 for the example)
  22.     canvas.create_text(75, 60, text=time)
  23.     if time == 0:
  24.         time=def_time+1
  25.         print('time out')
  26.         tick()
  27.     else:
  28.         canvas.after(1000, tick)
  29. canvas.after(1, tick)
  30. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement