-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
76 lines (49 loc) · 1.82 KB
/
timer.py
File metadata and controls
76 lines (49 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import time
from tkinter import *
from tkinter import messagebox
### Create interface object
clockWindow = Tk()
clockWindow.geometry("500x500")
clockWindow.title("Countdown Timer")
clockWindow.configure(background='dark grey')
### Declare variables
hourString = StringVar()
minuteString = StringVar()
secondString = StringVar()
### Set strings to a default value
hourString.set("00")
minuteString.set("00")
secondString.set("00")
### Get user input
hourTextbox = Entry(clockWindow, width=3, font=("Calibri", 20, ""), textvariable=hourString)
minuteTextbox = Entry(clockWindow, width=3, font=("Calibri", 20, ""), textvariable=minuteString)
secondTextbox = Entry(clockWindow, width=3, font=("Calibri", 20, ""), textvariable=secondString)
### Center textboxes
hourTextbox.place(x=170, y=180)
minuteTextbox.place(x=220, y=180)
secondTextbox.place(x=270, y=180)
def runTimer():
try:
clockTime = int(hourString.get())*3600 + int(minuteString.get())*60 + int(secondString.get())
except:
print("Can't do that")
while(clockTime > -1):
totalMinutes, totalSeconds = divmod(clockTime, 60)
totalHours = 0
if(totalMinutes > 60):
totalHours, totalMinutes = divmod(totalMinutes, 60)
hourString.set("{0:2d}".format(totalHours))
minuteString.set("{0:2d}".format(totalMinutes))
secondString.set("{0:2d}".format(totalSeconds))
### Update the interface
clockWindow.update()
time.sleep(1)
### Let user know if the timer has expired
if(clockTime == 0):
messagebox.showinfo("HEY!", "Your time has run out!")
clockTime -= 1
### adding button
setTimeButton = Button(clockWindow, text='Set Time', bd='5', command=runTimer)
setTimeButton.place(relx=0.5, rely=0.5, anchor=CENTER)
### Keep looping
clockWindow.mainloop()