1
+ import tkinter as tk
2
+ from tkinter import ttk
3
+ from datetime import datetime
4
+ import pygame
5
+
6
+ '''
7
+ Add .wav to hear your fav song as alarm sound
8
+ And enter you favorite song
9
+
10
+ '''
11
+ pygame .mixer .init (42050 , - 16 , 2 , 2048 )
12
+ alarm_sound = pygame .mixer .Sound ("" ) # add you .wav here
13
+ #Setting our initial global values
14
+ start_printed = False
15
+ stop_printed = True
16
+ done = False
17
+ finished = False
18
+ stop_clicked = False
19
+
20
+ class AlarmApp (tk .Tk ):
21
+ def __init__ (self ):
22
+ tk .Tk .__init__ (self )
23
+ #Title of the window set to 'Alarm Clock'
24
+
25
+ self .title ("Alarm Clock" )
26
+ #Make it so user can't resize page
27
+ self .resizable (width = False , height = False )
28
+ #Set up all of the drop-down lists
29
+ self .hr = tk .IntVar (self )
30
+ self .min = tk .IntVar (self )
31
+ self .ampm = tk .StringVar (self )
32
+ #Set the initial values of each drop-down list
33
+ self .hr .set ('12' )
34
+ self .min .set ("00" )
35
+ self .ampm .set ("AM" )
36
+ #Create the list of values from which we are choosing from in our drop-down list
37
+ hours = []
38
+ minutes = []
39
+ ampmlist = ["AM" ,"PM" ]
40
+ #Hours go from 1 to 12
41
+ for x in range (1 ,13 ):
42
+ hours .append (x )
43
+ #Minutes go from 0 to 59
44
+ for y in range (0 ,60 ):
45
+ minutes .append ("%02d" % (y ,))
46
+ #Placing all of our list into the respective drop-down list
47
+ self .popmenuhours = tk .OptionMenu (self ,self .hr , * hours )
48
+ self .popmenuminutes = tk .OptionMenu (self ,self .min , * minutes )
49
+ self .popmenuAMPM = tk .OptionMenu (self , self .ampm , * ampmlist )
50
+ #Placing our drop-down lists on the page as well as one label
51
+ self .popmenuhours .pack (side = "left" )
52
+ self .thing = tk .Label (text = ":" ).pack (side = "left" )
53
+ self .popmenuminutes .pack (side = "left" )
54
+ self .popmenuAMPM .pack (side = "left" )
55
+ #Setting up all the buttons on the right hand side of the window. The text refers to what the button says
56
+ #Command refers to which function it will run once it's clicked
57
+ #State refers to whether it is clickable or not at the current state.
58
+ self .alarmbutton = tk .Button (self , text = "Set Alarm" , command = self .start_clock )
59
+ #I disabled both of these buttons since they should only be able to be pressed when it is appropriate and the alarm is running
60
+ self .cancelbutton = tk .Button (self , text = "Cancel Alarm" , command = self .stop_clock , state = "disabled" )
61
+ self .stopalarmbutton = tk .Button (self , text = "Stop Alarm" , command = self .stop_audio , state = "disabled" )
62
+ #Packing all the buttons into the page
63
+ self .alarmbutton .pack ()
64
+ self .cancelbutton .pack ()
65
+ self .stopalarmbutton .pack ()
66
+
67
+ def start_clock (self ):
68
+
69
+ global done , start_printed , stop_printed , stop_clicked
70
+ #Done refers to whether either the time has been reached or if the user has cancelled. I.e: Loop is done.
71
+ if done == False :
72
+ #Cancel button is now active so user can decide at any point to cancel the alarm
73
+ self .cancelbutton .config (state = "active" )
74
+ #Alarm button is now disabled since an alarm has currently already been set
75
+ self .alarmbutton .config (state = "disabled" )
76
+ #On the first run of the loop, let the user know that an alarm has been set for their desired time
77
+ if start_printed == False :
78
+ #Print this notification for the user in the terminal
79
+ print ("Alarm set for {}:{}{}" .format (self .hr .get (), "%02d" % (self .min .get ()),self .ampm .get ()))
80
+ #Now set this to true, since we have printed it, so that it doesn't print it again on every loop for this set alarm
81
+ start_printed = True
82
+ #Stop printed set to false so that once the user cancels the timer, it will print a message (As we'll see later in the code)
83
+ stop_printed = False
84
+ #These next two if-statements are converting our hours from our drop-down list into 24-hour time, so that we can use it through DateTime
85
+ if self .ampm .get () == "AM" :
86
+ if self .hr .get () in range (1 ,12 ):
87
+ hour_value = self .hr .get ()
88
+ else :
89
+ hour_value = self .hr .get () - 12
90
+ if self .ampm .get () == "PM" :
91
+ if self .hr .get () in range (1 ,12 ):
92
+ hour_value = self .hr .get () + 12
93
+ else :
94
+ hour_value = self .hr .get ()
95
+ #Now we call the Alarm function with the information that the user has entered to check whether we have reached the alarm time
96
+ self .Alarm ("%02d" % (hour_value ,), "%02d" % (self .min .get ()))
97
+ #If user has clicked the cancel alarm button, we reset everything
98
+ if stop_clicked == True :
99
+ done = False
100
+ start_printed = False
101
+ stop_clicked = False
102
+
103
+ def stop_clock (self ):
104
+ global done , stop_clicked
105
+ #Let the user know that the alarm has been cancelled by printing it in the terminal
106
+ print ("Alarm set for {}:{}{} has been cancelled" .format (self .hr .get (), "%02d" % (self .min .get ()),self .ampm .get ()))
107
+ #Cancel button has now been clicked
108
+ stop_clicked = True
109
+ #Now done with the current alarm/loop
110
+ done = True
111
+ #Buttons reset to what they were originally
112
+ self .cancelbutton .config (state = "disabled" )
113
+ self .alarmbutton .config (state = "active" )
114
+
115
+ def stop_audio (self ):
116
+ #Use PyGame to stop the audio since button has been clicked
117
+ pygame .mixer .Sound .stop (alarm_sound )
118
+ #Stop alarm button disabled and alarm button active, essentially reseting everything
119
+ self .stopalarmbutton .config (state = "disabled" )
120
+ self .alarmbutton .config (state = "active" )
121
+
122
+
123
+
124
+ def Alarm (self ,myhour ,myminute ):
125
+ global done , start_printed , finished
126
+ #If we are still not done, we follow this statement
127
+ if done == False :
128
+ #We convert the information into strings (To match DateTime)
129
+ myhour ,myminute = str (myhour ),str (myminute )
130
+ #Next, we extract the data of the current time from DateTime and take the information we want (hour and minute)
131
+ a = str (datetime .now ())
132
+ b = a .split (" " )[1 ].split (":" )
133
+ hour = b [0 ]
134
+ minute = b [1 ]
135
+ #Now, if the alarm time matches the current time, we follow his statement. Alarm is going to go off!
136
+ if hour == myhour and minute == myminute :
137
+ #Using pygame to play audio, loops = -1 refers to an infinite loop
138
+ pygame .mixer .Sound .play (alarm_sound , loops = - 1 )
139
+ print ("Alarm is ringing!" )
140
+ #We are now done
141
+ done = True
142
+ #Also finished
143
+ finished = True
144
+ #Now we change back the state of the cancel button to disabled, and the state of the alarm stop to active
145
+ #This is so the user can stop the alarm, since it will infinitely loop
146
+ self .cancelbutton .config (state = "disabled" )
147
+ self .stopalarmbutton .config (state = "active" )
148
+
149
+ else :
150
+ #If it is still not the set time, we recursively loop back to the start_clock function
151
+ self .after (1000 , self .start_clock )
152
+ done = False
153
+ #If we are finished, which we are when the alarm goes off, we reset everything
154
+ if finished == True :
155
+ start_printed = False
156
+ finished = False
157
+
158
+ app = AlarmApp ()
159
+ app .mainloop ()
0 commit comments