Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #source https://bit.ly/2U0TT0J
- from tkinter import *
- root = Tk()
- root.geometry("150x120")
- canvas = Canvas(root)
- canvas.pack()
- def_time=10
- time = def_time
- def tick():
- # You have to clear the canvas each time the clock updates
- # (otherwise it writes on top of the old time). Since the
- # time is the only thing in the canvas, delete(ALL) works
- # perfectly (if it wasn't however, you can delete the id
- # that goes with the clock).
- canvas.delete(ALL)
- # I have to declare time as a global because I'm not using
- # a class (otherwise, I could do something like self.time -= 1)
- global time
- time -= 1
- # You can place the time wherever in the canvas
- # (I chose 10,10 for the example)
- canvas.create_text(75, 60, text=time)
- if time == 0:
- time=def_time+1
- print('time out')
- tick()
- else:
- canvas.after(1000, tick)
- canvas.after(1, tick)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement