Introduction
An Alarm clock is a very useful thing in our busy life. It helps to people to get notified when they are in deep sleep or taking a power nap, etc.
Sometimes people use alarms as reminders also. In the past, people used to keep a table clock at home and set alarms on it. At present time, smartwatches are coming with this feature. But the majority of people use default mobile applications to set alarm times.
In this tutorial, I will guide you to build an Advanced Alarm Clock using Python. For easy to use, we'll give a graphical interface to this project using the Tkinter library.
👉Visit Also: Meditation App in Python Tkinter: Practice Deep Breathing
The Project Details
The project contains two folders and one python program file(AlarmClock.py) in the main folder. One is the "Ringtones" folder(contains several ringtone choices) and another one is the "Images" folder(One image is there for the background).
The program file must require these two folders and the other files inside there.
Look at the image below. It's the first window of our Alarm Clock. First, it displays the current time and the name of the weekday.
Figure 1: Displaying the Time |
To set an alarm time you need to go through some easy steps. These are given below.
Figure 2: Alarm Clock Window |
Steps
1. First, click the "Set Alarm" button,
2. Now set the alarm hour and minute to get notified,
3. Choose a ringtone from the several options,
4. Type a message that you want to see when You will get notified,
5. Now You can test the alarm ringtone by pressing the test button,
There are two more buttons, one is for "Cancel" and another for "Start".
6. Click the "Start" button to set the alarm time.
After pressing the "Start" button you will get a notification window that will show the alarm time that you have set.
You can download the zip file of this project from my GitHub Page(https://github.com/subhankar-rakshit) through the Download button given below.
Requirements and Installation
🔹tkinter: pip install tk (For Graphical User Interface)
🔹playsound: pip install playsound (For playing the ringtones)
Import the Modules
Let's create a Python file with the name "AlarmClock.py" and start writing the code by importing these modules.
'''
Python Projects:
Advanced Alarm Clock using Python Tkinter
'''
from tkinter import *
import time
from PIL import ImageTk
from tkinter import ttk, messagebox
from playsound import playsound
import multiprocessing
from datetime import datetime
from threading import *
Declare Three Lists: For Hours, Minutes, and Ringtones
'hours_list' contains 0-24 numbers to represent hours, 'minutes_list' contains 0-60 numbers to represent minutes, and 'ringtones_list' is containing the name of the ringtones the users can set as per their choices.
# Hours List.
hours_list = ['00', '01', '02', '03', '04', '05', '06', '07',
'08', '09', '10', '11', '12', '13', '14', '15',
'16', '17', '18', '19', '20', '21', '22', '23', '24']
# Minutes List.
minutes_list = ['00', '01', '02', '03', '04', '05', '06', '07',
'08', '09', '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']
# Ringtones list.
ringtones_list = ['mom_calling', 'nice_wake_up', 'romantic',
'twirling_intime', 'wakeup_alarm_tone']
Set the Ringtones Paths
We've declared the ringtones' names just before. Now you need to set the path for each ringtone name. Remember, I told you about the 'Ringtones' folder which contains a bunch of .mp3 files for ringtones(You can modify it too). Here, we're mentioning paths in the 'ringtones_path' dictionary.
# Ringtones Paths.
ringtones_path = {
'mom_calling': 'Ringtones/mom_calling.mp3',
'nice_wake_up': 'Ringtones/nice_wake_up.mp3',
'romantic': 'Ringtones/romantic.mp3',
'twirling_intime': 'Ringtones/twirling_intime.mp3',
'wakeup_alarm_tone': 'Ringtones/wakeup_alarm_tone.mp3'
}
Declare the First Window
Our Alarm Clock will show the current time and the weekday name in the first window(See Figure 1); so, we need to declare some widgets to represent that. Here is the code for it.
# The Class: Alarm Clock.
class Alarm_Clock:
def __init__(self, root):
self.window = root
self.window.geometry("680x420+0+0")
self.window.title("PyClock")
# Background image of the first window.
self.bg_image = ImageTk.PhotoImage(file="Images/image_1.jpg")
self.background = Label(self.window, image=self.bg_image)
self.background.place(x=0,y=0,relwidth=1,relheight=1)
# Display Label that shows the current time in the
# first window
self.display = Label(self.window, font=('Helvetica', 34),
bg = 'gray8', fg = 'yellow')
self.display.place(x=100,y=150)
# Calling the the function.
self.show_time()
# Placing the set alarm button.
# Font Type: relief solid font helevetica.
set_button = Button(self.window, text="Set Alarm",
font=('Helvetica',15), bg="green", fg="white",
command=self.another_window)
set_button.place(x=270, y=220)
Define a function for showing the Current Time
This function will show the live current time. It'll update the time every 100 milliseconds until the user takes another action.
# This function shows the current time in the first window.
def show_time(self):
current_time = time.strftime('%H:%M:%S %p, %A')
# Placing the time format level.
self.display.config(text = current_time)
self.display.after(100, self.show_time)
Define the Set Alarm Window
When the 'Set Alarm' button is pressed, this function will get a call. It will show the widgets related to setting an alarm time.
# Another Window: This window will show, when the "Set Alarm"
# Button will pressed.
def another_window(self):
self.window_2 = Tk()
self.window_2.title("Set Alarm")
self.window_2.geometry("680x420+200+200")
# Hour Label.
hours_label = Label(self.window_2, text="Hours",
font=("times new roman",20))
hours_label.place(x=150, y=50)
# Minute Label.
minute_label = Label(self.window_2, text="Minutes",
font=("times new roman",20))
minute_label.place(x=450, y=50)
# Hour Combobox.
self.hours = StringVar()
self.hours_combobox = ttk.Combobox(self.window_2,
width=10, height=10, textvariable=self.hours,
font=("times new roman",15))
self.hours_combobox['values'] = hours_list
self.hours_combobox.current(0)
self.hours_combobox.place(x=150,y=90)
# Minute Combobox.
self.minutes = StringVar()
self.minutes_combobox = ttk.Combobox(self.window_2,
width=10, height=10, textvariable=self.minutes,
font=("times new roman",15))
self.minutes_combobox['values'] = minutes_list
self.minutes_combobox.current(0)
self.minutes_combobox.place(x=450,y=90)
# Ringtone Label.
ringtone_label = Label(self.window_2, text="Ringtones",
font=("times new roman",20))
ringtone_label.place(x=150, y=130)
# Ringtone Combobox(Choose the ringtone).
self.ringtones = StringVar()
self.ringtones_combobox = ttk.Combobox(self.window_2, \
width=15, height=10, textvariable=self.ringtones, \
font=("times new roman",15))
self.ringtones_combobox['values'] = ringtones_list
self.ringtones_combobox.current(0)
self.ringtones_combobox.place(x=150,y=170)
# Title or Message Label.
message_label = Label(self.window_2, text="Message",
font=("times new roman",20))
message_label.place(x=150, y=210)
# Message Entrybox: This Message will show, when
# the alarm will ringing.
self.message = StringVar()
self.message_entry = Entry(self.window_2,
textvariable=self.message, font=("times new roman",14), width=30)
self.message_entry.insert(0, 'Wake Up')
self.message_entry.place(x=150, y=250)
# Test Button: For testing the ringtone music.
test_button = Button(self.window_2, text='Test', \
font=('Helvetica',15), bg="white", fg="black", command=self.test_window)
test_button.place(x=150, y=300)
# The Cancel Button: For cancel the alarm.
cancel_button = Button(self.window_2,
text='Cancel', font=('Helvetica',15), bg="white", \
fg="black", command=self.window_2.destroy)
cancel_button.place(x=390, y=300)
# The Start Button: For set the alarm time
start_button = Button(self.window_2, text='Start', \
font=('Helvetica',15), bg="green", fg="white", command=self.Threading_1)
start_button.place(x=490, y=300)
self.window_2.mainloop()
Multiprocessing and Thread
Declare a different function for playing the audio file as a ringtone. Here, a different process and thread will create for playing a selected ringtone(.mp3 file).
# In this function, I have used the python multiprocessing module
# to play the ringtones while the alarm gets notified.
def test_window(self):
process = multiprocessing.Process(target=playsound, \
args=(ringtones_path[self.ringtones_combobox.get()],))
process.start()
messagebox.showinfo('Playing...', 'press ENTER to stop playing')
process.terminate()
# Creating a thread
def Threading_1(self):
x = Thread(target=self.set_alarm_time)
x.start()
Define a function to set the Alarm Time
This function will get a call when the user will press the start button after setting all the requirements that our Alarm Clock offers to set.
# This function gets called when the start button pressed
# in another window for setting the alarm time.
def set_alarm_time(self):
alarm_time = f"{self.hours_combobox.get()}:{self.minutes_combobox.get()}"
messagebox.showinfo("Alarm Set",f"Alarm set for {alarm_time}")
while True:
# The current time is in 24 hour format
current_time = datetime.now()
# Converting the current time into hour and minute
current_time_format = current_time.strftime("%H:%M")
if current_time_format == alarm_time:
process = multiprocessing.Process(target=playsound, \
args=(ringtones_path[self.ringtones_combobox.get()],))
process.start()
# Messagebox: This messagebox will show, when the
# alarm will ringing.
messagebox.showinfo("Alarm",f"{self.message_entry.get()}, \
It's {alarm_time}")
process.terminate()
break
The main function
Now it's time to lead the whole program. Let's declare the main function for this.
# The main function.
if __name__ == "__main__":
root = Tk()
# Object of Alarm_Clock class.
obj = Alarm_Clock(root)
root.mainloop()
Full Code
Here is the full code for your convenience.
'''
Python Projects:
Advanced Alarm Clock using Python Tkinter
'''
from tkinter import *
import time
from PIL import ImageTk
from tkinter import ttk, messagebox
from playsound import playsound
import multiprocessing
from datetime import datetime
from threading import *
# Hours List.
hours_list = ['00', '01', '02', '03', '04', '05', '06', '07',
'08', '09', '10', '11', '12', '13', '14', '15',
'16', '17', '18', '19', '20', '21', '22', '23', '24']
# Minutes List.
minutes_list = ['00', '01', '02', '03', '04', '05', '06', '07',
'08', '09', '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']
# Ringtones list.
ringtones_list = ['mom_calling', 'nice_wake_up', 'romantic',
'twirling_intime', 'wakeup_alarm_tone']
# Ringtones Path.
ringtones_path = {
'mom_calling': 'Ringtones/mom_calling.mp3',
'nice_wake_up': 'Ringtones/nice_wake_up.mp3',
'romantic': 'Ringtones/romantic.mp3',
'twirling_intime': 'Ringtones/twirling_intime.mp3',
'wakeup_alarm_tone': 'Ringtones/wakeup_alarm_tone.mp3'
}
# The Class: Alarm Clock.
class Alarm_Clock:
def __init__(self, root):
self.window = root
self.window.geometry("680x420+0+0")
self.window.title("PyClock")
# Background image of the first window.
self.bg_image = ImageTk.PhotoImage(file="Images/image_1.jpg")
self.background = Label(self.window, image=self.bg_image)
self.background.place(x=0,y=0,relwidth=1,relheight=1)
# Display Label that shows the current time in the
# first window
self.display = Label(self.window, font=('Helvetica', 34),
bg = 'gray8', fg = 'yellow')
self.display.place(x=100,y=150)
# Calling the the function.
self.show_time()
# Placing the set alarm button.
# Font Type: relief solid font helevetica.
set_button = Button(self.window, text="Set Alarm",
font=('Helvetica',15), bg="green", fg="white",
command=self.another_window)
set_button.place(x=270, y=220)
# This function shows the current time in the first window.
def show_time(self):
current_time = time.strftime('%H:%M:%S %p, %A')
# Placing the time format level.
self.display.config(text = current_time)
self.display.after(100, self.show_time)
# Another Window: This window will show, when the "Set Alarm"
# Button will pressed.
def another_window(self):
self.window_2 = Tk()
self.window_2.title("Set Alarm")
self.window_2.geometry("680x420+200+200")
# Hour Label.
hours_label = Label(self.window_2, text="Hours",
font=("times new roman",20))
hours_label.place(x=150, y=50)
# Minute Label.
minute_label = Label(self.window_2, text="Minutes",
font=("times new roman",20))
minute_label.place(x=450, y=50)
# Hour Combobox.
self.hours = StringVar()
self.hours_combobox = ttk.Combobox(self.window_2,
width=10, height=10, textvariable=self.hours,
font=("times new roman",15))
self.hours_combobox['values'] = hours_list
self.hours_combobox.current(0)
self.hours_combobox.place(x=150,y=90)
# Minute Combobox.
self.minutes = StringVar()
self.minutes_combobox = ttk.Combobox(self.window_2,
width=10, height=10, textvariable=self.minutes,
font=("times new roman",15))
self.minutes_combobox['values'] = minutes_list
self.minutes_combobox.current(0)
self.minutes_combobox.place(x=450,y=90)
# Ringtone Label.
ringtone_label = Label(self.window_2, text="Ringtones",
font=("times new roman",20))
ringtone_label.place(x=150, y=130)
# Ringtone Combobox(Choose the ringtone).
self.ringtones = StringVar()
self.ringtones_combobox = ttk.Combobox(self.window_2, \
width=15, height=10, textvariable=self.ringtones, \
font=("times new roman",15))
self.ringtones_combobox['values'] = ringtones_list
self.ringtones_combobox.current(0)
self.ringtones_combobox.place(x=150,y=170)
# Title or Message Label.
message_label = Label(self.window_2, text="Message",
font=("times new roman",20))
message_label.place(x=150, y=210)
# Message Entrybox: This Message will show, when
# the alarm will ringing.
self.message = StringVar()
self.message_entry = Entry(self.window_2,
textvariable=self.message, font=("times new roman",14), width=30)
self.message_entry.insert(0, 'Wake Up')
self.message_entry.place(x=150, y=250)
# Test Button: For testing the ringtone music.
test_button = Button(self.window_2, text='Test', \
font=('Helvetica',15), bg="white", fg="black", command=self.test_window)
test_button.place(x=150, y=300)
# The Cancel Button: For cancel the alarm.
cancel_button = Button(self.window_2,
text='Cancel', font=('Helvetica',15), bg="white", \
fg="black", command=self.window_2.destroy)
cancel_button.place(x=390, y=300)
# The Start Button: For set the alarm time
start_button = Button(self.window_2, text='Start', \
font=('Helvetica',15), bg="green", fg="white", command=self.Threading_1)
start_button.place(x=490, y=300)
self.window_2.mainloop()
# In this function, I have used python multiprocessing module
# to play the ringtones while the alarm gets notified.
def test_window(self):
process = multiprocessing.Process(target=playsound, \
args=(ringtones_path[self.ringtones_combobox.get()],))
process.start()
messagebox.showinfo('Playing...', 'press ENTER to stop playing')
process.terminate()
# Creating a thread
def Threading_1(self):
x = Thread(target=self.set_alarm_time)
x.start()
# This function gets called when the start button pressed
# in the another window for setting alarm time.
def set_alarm_time(self):
alarm_time = f"{self.hours_combobox.get()}:{self.minutes_combobox.get()}"
messagebox.showinfo("Alarm Set",f"Alarm set for {alarm_time}")
while True:
# The current time is in 24 hour format
current_time = datetime.now()
# Converting the current time into hour and minute
current_time_format = current_time.strftime("%H:%M")
if current_time_format == alarm_time:
process = multiprocessing.Process(target=playsound, \
args=(ringtones_path[self.ringtones_combobox.get()],))
process.start()
# Messagebox: This messagebox will show, when the
# alarm will ringing.
messagebox.showinfo("Alarm",f"{self.message_entry.get()}, \
It's {alarm_time}")
process.terminate()
break
# The main function.
if __name__ == "__main__":
root = Tk()
# Object of Alarm_Clock class.
obj = Alarm_Clock(root)
root.mainloop()
How to run this python project
Watch the entire video to perceive how the application works.
Output Video
Download The Source Code
Download the Source Code from my GitHub page(https://github.com/subhankar-rakshit) through the Download button.
👉Visit Also: Create an Image Viewer Application using Python
Summary
There are many applications available online, through which you can set alarm times. But as a python programmer, it is quite funny to develop such kind of application using our favorite programming language.
In this tutorial, we build an alarm clock project in python. It's quite simple and easy to use.
You can see the current time through it and set an alarm time using tkinter widgets. This application will offer you to choose a ringtone from several choices and you can listen it too before setting it for an alarm time.
There are more lovely Python Projects that I have created with the Tkinter library. Here are some examples of them.
👉Language Translator App with Python Tkinter - Google Translate
👉Countdown Timer in Python - with Start and Pause Function
👉Create an Age Calculator in Python using Tkinter Library
If you get any error messages while running this project, just drop your comment below with the full description of the error message. You will get a reply soon.
Thanks for reading!💙
PySeek