python countdown timer tkinter

Solutions on MaxInterview for python countdown timer tkinter by the best coders in the world

showing results for - "python countdown timer tkinter"
Linus
01 Jul 2020
1import time
2from tkinter import *
3from tkinter import messagebox
4
5
6f = ("Arial",24)
7
8ws = Tk()
9ws.geometry("300x250+1500+700")
10ws.title("PythonGuides")
11ws.config(bg='#345')
12
13hour=StringVar()
14minute=StringVar()
15second=StringVar()
16
17hour.set("00")
18minute.set("00")
19second.set("10")
20
21hour_tf= Entry(
22	ws, 
23	width=3, 
24	font=f,
25	textvariable=hour
26	)
27
28hour_tf.place(x=80,y=20)
29
30mins_tf= Entry(
31	ws, 
32	width=3, 
33	font=f,
34	textvariable=minute)
35
36mins_tf.place(x=130,y=20)
37
38sec_tf = Entry(
39	ws, 
40	width=3, 
41	font=f,
42	textvariable=second)
43
44sec_tf.place(x=180,y=20)
45
46
47def startCountdown():
48	try:
49		userinput = int(hour.get())*3600 + int(minute.get())*60 + int(second.get())
50	except:
51		messagebox.showwarning('', 'Invalid Input!')
52	while userinput >-1:
53		mins,secs = divmod(userinput,60) 
54
55		hours=0
56		if mins >60:
57			
58		
59			hours, mins = divmod(mins, 60)
60	
61		hour.set("{0:2d}".format(hours))
62		minute.set("{0:2d}".format(mins))
63		second.set("{0:2d}".format(secs))
64
65	
66		ws.update()
67		time.sleep(1)
68
69	
70		if (userinput == 0):
71			messagebox.showinfo("", "Time's Up")
72		
73
74		userinput -= 1
75
76start_btn = Button(
77	ws, 
78	text='START', 
79	bd='5',
80	command= startCountdown
81	)
82
83start_btn.place(x = 120,y = 120)
84
85
86ws.mainloop()