Skip to content

Commit 79a1a88

Browse files
committed
add alarm clock app tutorial
1 parent 61b8530 commit 79a1a88

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
243243
- [How to Make a Rich Text Editor with Tkinter in Python](https://www.thepythoncode.com/article/create-rich-text-editor-with-tkinter-python). ([code](gui-programming/rich-text-editor))
244244
- [How to Make a Python Code Editor using Tkinter in Python](https://www.thepythoncode.com/article/python-code-editor-using-tkinter-python). ([code](gui-programming/python-code-editor/))
245245
- [How to Make an Age Calculator in Python](https://www.thepythoncode.com/article/age-calculator-using-tkinter-python). ([code](gui-programming/age-calculator))
246+
- [How to Create an Alarm Clock App using Tkinter in Python](https://www.thepythoncode.com/article/build-an-alarm-clock-app-using-tkinter-python). ([code](gui-programming/alarm-clock-app))
246247

247248
For any feedback, please consider pulling requests.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Create an Alarm Clock App using Tkinter in Python](https://www.thepythoncode.com/article/build-an-alarm-clock-app-using-tkinter-python)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from tkinter import *
2+
import datetime
3+
import time
4+
from playsound import playsound
5+
from tkinter import messagebox
6+
from threading import *
7+
8+
9+
root = Tk() # initializes tkinter to create display window
10+
root.geometry('450x250') # width and height of the window
11+
root.resizable(0, 0) # sets fix size of window
12+
root.title(' Alarm Clock') # gives the window a title
13+
14+
15+
addTime = Label(root, fg="red", text="Hour Min Sec",
16+
font='arial 12 bold').place(x=210)
17+
setYourAlarm = Label(root, text="Set Time(24hrs): ",
18+
bg="grey", font="arial 11 bold").place(x=80, y=40)
19+
hour = StringVar()
20+
min = StringVar()
21+
sec = StringVar()
22+
23+
# make the time input fields
24+
hourTime = Entry(root, textvariable=hour, relief=RAISED, width=4, font=(20)).place(x=210, y=40)
25+
minTime = Entry(root, textvariable=min, width=4, font=(20)).place(x=270, y=40)
26+
secTime = Entry(root, textvariable=sec, width=4, font=(20)).place(x=330, y=40)
27+
28+
29+
def start_alarm():
30+
t1 = Thread(target=alarm)
31+
t1.start()
32+
33+
34+
def alarm():
35+
while True:
36+
set_alarm_time = f"{hour.get()}:{min.get()}:{sec.get()}"
37+
# sleep for 1s to update the time every second
38+
time.sleep(1)
39+
# Get current time
40+
actual_time = datetime.datetime.now().strftime("%H:%M:%S")
41+
FMT = '%H:%M:%S'
42+
# get time remaining
43+
time_remaining = datetime.datetime.strptime(
44+
set_alarm_time, FMT) - datetime.datetime.strptime(actual_time, FMT)
45+
# displays current time
46+
CurrentLabel = Label(
47+
root, text=f'Current time: {actual_time}', fg='black')
48+
CurrentLabel.place(relx=0.2, rely=0.8, anchor=CENTER)
49+
# displays alarm time
50+
AlarmLabel = Label(
51+
root, text=f'Alarm time: {set_alarm_time}', fg='black')
52+
AlarmLabel.place(relx=0.2, rely=0.9, anchor=CENTER)
53+
# displays time remaining
54+
RemainingLabel = Label(
55+
root, text=f'Remaining time: {time_remaining}', fg='red')
56+
RemainingLabel.place(relx=0.7, rely=0.8, anchor=CENTER)
57+
# Check whether set alarm is equal to current time
58+
if actual_time == set_alarm_time:
59+
# Playing sound
60+
playsound('audio.mp3')
61+
messagebox.showinfo("TIME'S UP!!!")
62+
63+
64+
# create a button to set the alarm
65+
submit = Button(root, text="Set Your Alarm", fg="red", width=20,
66+
command=start_alarm, font=("arial 20 bold")).pack(pady=80, padx=120)
67+
# run the program
68+
root.mainloop()
36.6 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
playsound

0 commit comments

Comments
 (0)