-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalarm_clock.py
More file actions
191 lines (146 loc) · 6.03 KB
/
alarm_clock.py
File metadata and controls
191 lines (146 loc) · 6.03 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from tkinter import *
import time
from datetime import *
class Clock(object):
def main(self):
#Initiate clock
self.clock = Tk()
self.clock.wm_title("Alarm Clock")
self.clock.geometry("500x500")
self.title_text = Label(self.clock,text = "Alarm Clock", fg = "black", font = 50)
self.title_text.place(x=200, y=0)
#Icon
self.icon = PhotoImage(file='images/clock.png')
self.clock.iconphoto(False,self.icon)
#Set variables
self.Hours = IntVar()
self.Minutes = IntVar()
self.Seconds = IntVar()
self.Expected_time = StringVar()
self.Timer = StringVar()
#Input of time; Entry
self.set_time_hour = Entry(self.clock, textvariable=self.Hours)
self.set_time_hour.place(x= 50, y= 100, height = 50, width = 50)
self.set_time_minute = Entry(self.clock, textvariable=self.Minutes)
self.set_time_minute.place(x= 200, y =100, height= 50, width = 50)
self.set_time_sec = Entry(self.clock,textvariable=self.Seconds)
self.set_time_sec.place(x=350,y=100, height=50, width = 50)
#Display the time and the expected time to alarm
self.show_countdown = Entry(self.clock, textvariable=self.Timer,width = 10)
self.show_countdown.place (x=200, y = 330, height = 30)
self.show_expected_time = Entry(self.clock, textvariable=self.Expected_time, width = 10)
self.show_expected_time.place (x=200, y= 400, height = 30)
#Display text
self.countdown_text = Label(self.clock, text='Time Remaining')
self.countdown_text.place(x =200, y = 310)
self.expected_time_text = Label(self.clock, text = 'Time to alarm')
self.expected_time_text.place(x=200, y=380)
self.hours_text = Label(self.clock, text='Hours')
self.hours_text.place(x=50,y=80)
self.minutes_text = Label(self.clock, text='Minutes')
self.minutes_text.place(x=200,y=80)
self.seconds_text = Label(self.clock, text='Seconds')
self.seconds_text.place(x=350,y=80)
#Buttons
self.start_timer = Button(self.clock, text='Start Timer', command = self.retrieved_time, width =10)
self.start_timer.place(x=200, y = 210)
self.stop_timer = Button(self.clock, text='Stop Timer', command= self.reset, width =10)
self.stop_timer.place(x = 200, y =260)
self.pause_button = Button(self.clock, text = ' Pause Timer', command =self.pause, width = 10)
self.pause_button.place(x= 200, y=160)
self.pause_button.config(state='disabled')
self.stop_timer.config(state='disabled')
#Main loop
self.clock.update()
self.clock.mainloop()
def retrieved_time(self):
try:
#Get time
self.set_hour = self.Hours.get()
self.set_minute = self.Minutes.get()
self.set_second = self.Seconds.get()
#Checks if the input are zero
if self.set_hour or self.set_minute or self.set_second > 0:
#Disable button
self.start_timer.config(state='disabled')
self.pause_button.config(state='normal')
self.stop_timer.config(state='normal')
self.set_time_hour.config(state='disabled')
self.set_time_minute.config(state='disabled')
self.set_time_sec.config(state='disabled')
self.time_computation(self.set_hour, self.set_minute, self.set_second)
except Exception:
#When user input wrong variables
self.error_window = Toplevel()
self.error_window.wm_title('Error')
self.error_window.geometry('250x150')
self.error_icon = PhotoImage(file='images/error.png')
self.error_window.iconphoto(False, self.error_icon)
self.error_window.grab_set()
self.error_message = Label(self.error_window, text = 'Please put numbers',font = 40)
self.error_message.place(x = 40, y =0)
self.close_button = Button(self.error_window, text='Close', command = self.error_window.destroy,width=20)
self.close_button.place(x=30, y = 80)
def time_computation (self,set_hour, set_minute, set_second):
# Getting current time and calculate expected time
self.current_time = datetime.now()
self.set_timer = timedelta(hours=self.set_hour,minutes=self.set_minute,seconds=self.set_second)
self.added_time = self.current_time + self.set_timer
self.expected_time_alarm = self.added_time.strftime('%H:%M:%S')
self.Expected_time.set(self.expected_time_alarm)
self.countdown()
def countdown(self):
#Decrease countdown
self.set_timer -= timedelta(seconds=1)
self.counter = self.clock.after(1000,self.countdown)
self.Timer.set(self.set_timer)
#Condition if the timer is met with 0; timer undergoes reset
if self.set_timer == timedelta(hours=0,minutes=0,seconds=0):
self.reset()
self.pop_up()
def reset(self):
#Stops the countdown
self.clock.after_cancel(self.counter)
#Reset variables
self.Hours.set(0)
self.Minutes.set(0)
self.Seconds.set(0)
self.Timer.set('')
self.Expected_time.set('')
#Reset button state
self.start_timer.config(state="normal")
self.pause_button.config(state="disabled")
#Reset Entry
self.set_time_hour.config(state="normal")
self.set_time_minute.config(state="normal")
self.set_time_sec.config(state="normal")
def pop_up(self):
#New window
self.alarm_window = Toplevel()
self.alarm_window.wm_title('Alarm')
self.alarm_window.geometry('250x150')
#Disable interaction of the lower window
self.alarm_window.grab_set()
self.icon_alarm = PhotoImage(file='images/clock.png')
self.alarm_window.iconphoto(False,self.icon_alarm)
self.words = Label(self.alarm_window, text = 'Times up!',font = 40)
self.words.place(x = 85, y =0)
self.close_button = Button(self.alarm_window, text='Close', command = self.alarm_window.destroy,width=20)
self.close_button.place(x=30, y = 80)
def pause(self):
self.clock.after_cancel(self.counter)
self.string = str(self.set_timer)
self.split_string = self.string.split(":")
self.paused_hour = self.split_string[0]
self.paused_minute = self.split_string[1]
self.paused_second = self.split_string[2]
self.Hours.set(int(self.paused_hour))
self.Minutes.set(int(self.paused_minute))
self.Seconds.set(int(self.paused_second))
self.Timer.set('')
self.Expected_time.set('')
self.start_timer.config(state="normal")
self.pause_button.config(state="disabled") #This is to prevent getting NULL
if __name__ == "__main__":
start = Clock()
start.main()