Have you ever wanted to build your own alarm clock using Python? In this tutorial, we’ll create a fully working alarm clock with sound using Tkinter, Pillow, Pygame mixer, and Threading.
We’ll do this in two parts:
-
Designing the Layout (UI) – creating the graphical interface.
-
Adding Logic – making the alarm functional with sound and time checks.
By the end, you’ll have a Python alarm clock that plays music at a set time.
Part 1: Designing the Layout
We’ll use Tkinter to create the GUI.
Step 1: Import required modules
from tkinter.ttk import *
from tkinter import *
from PIL import ImageTk, Image
Step 2: Create the main Tkinter window
# colors
bg_color = '#ffffff'
co1 = "#566FC6" # blue
co2 = "#000000" # black
# window
window = Tk()
window.title("Python Alarm Clock")
window.geometry('350x150')
window.configure(bg=bg_color)
Step 3: Add frames
We’ll create two frames:
-
A top line (blue bar)
-
A main body for widgets
frame_line = Frame(window, width=400, height=5, bg=co1)
frame_line.grid(row=0, column=0)
frame_body = Frame(window, width=400, height=290, bg=bg_color)
frame_body.grid(row=1, column=0)
Step 4: Add alarm icon and title
# load image
img = Image.open('icon.png')
img.resize((100, 100))
img = ImageTk.PhotoImage(img)
# place image
app_image = Label(frame_body, height=100, image=img, bg=bg_color)
app_image.place(x=10, y=10)
# label
name = Label(frame_body, text="Alarm", font=('Ivy 18 bold'), bg=bg_color)
name.place(x=125, y=10)
Step 5: Add input fields with Combobox
We’ll use Combobox for hour, minute, second, and AM/PM.
# Hour
hour = Label(frame_body, text="hour", font=('Ivy 10 bold'), bg=bg_color, fg=co1)
hour.place(x=127, y=40)
c_hour = Combobox(frame_body, width=2, font=('arial 15'))
c_hour['values'] = ("00","01","02","03","04","05","06","07","08","09","10","11","12")
c_hour.current(0)
c_hour.place(x=130, y=58)
# Minute
min = Label(frame_body, text="min", font=('Ivy 10 bold'), bg=bg_color, fg=co1)
min.place(x=177, y=40)
c_min = Combobox(frame_body, width=2, font=('arial 15'))
c_min['values'] = tuple(f"{i:02}" for i in range(60))
c_min.current(0)
c_min.place(x=180, y=58)
# Second
sec = Label(frame_body, text="sec", font=('Ivy 10 bold'), bg=bg_color, fg=co1)
sec.place(x=227, y=40)
c_sec = Combobox(frame_body, width=2, font=('arial 15'))
c_sec['values'] = tuple(f"{i:02}" for i in range(60))
c_sec.current(0)
c_sec.place(x=230, y=58)
# Period
period = Label(frame_body, text="period", font=('Ivy 10 bold'), bg=bg_color, fg=co1)
period.place(x=277, y=40)
c_period = Combobox(frame_body, width=3, font=('arial 15'))
c_period['values'] = ("AM", "PM")
c_period.current(0)
c_period.place(x=280, y=58)
Step 6: Add Activate button
We’ll use a Radiobutton to activate the alarm.
selected = IntVar()
rad1 = Radiobutton(frame_body, font=('arial 10 bold'), value=1,
text="Activate", bg=bg_color,
variable=selected)
rad1.place(x=125, y=95)
👉 Now our UI is ready! Next, let’s add the logic.
Part 2: Adding the Logic
Now we’ll use pygame mixer, datetime, and threading to make the alarm functional.
Step 7: Import required modules for logic
from pygame import mixer
from datetime import datetime
from time import sleep
from threading import Thread
mixer.init()
Step 8: Function to play sound
def sound_alarm():
mixer.music.load('alarm2.mp3') # your mp3 file
mixer.music.play()
selected.set(0)
rad2 = Radiobutton(frame_body, font=('arial 10 bold'), value=2,
text="Deactivate", bg=bg_color,
command=deactivate_alarm,
variable=selected)
rad2.place(x=187, y=95)
Step 9: Function to deactivate alarm
def deactivate_alarm():
print('Deactivated alarm: ', selected.get())
mixer.music.stop()
Step 10: Alarm function (runs continuously)
def alarm():
while True:
control = selected.get()
alarm_hour = c_hour.get()
alarm_minute = c_min.get()
alarm_sec = c_sec.get()
alarm_period = c_period.get().upper()
now = datetime.now()
hour = now.strftime("%I")
minute = now.strftime("%M")
second = now.strftime("%S")
period = now.strftime("%p")
if control == 1:
if (alarm_period == period and
alarm_hour == hour and
alarm_minute == minute and
alarm_sec == second):
print("Time to take a break!")
sound_alarm()
sleep(1)
Step 11: Activate alarm with threading
def activate_alarm():
t = Thread(target=alarm)
t.start()
rad1.config(command=activate_alarm)
Step 12: Run the Tkinter main loop
window.mainloop()
Final Output
✅ When you run the project:
-
Select your desired time using the comboboxes.
-
Click Activate.
-
When the current time matches, 🎵 the alarm sound will play.
-
Click Deactivate to stop it.
from threading import Thread
from tkinter.ttk import *
from tkinter import *
from PIL import ImageTk, Image
from pygame import mixer
from datetime import datetime
from time import sleep
# colors
bg_color = '#ffffff'
co1 = "#566FC6" # blue
co2 = "#000000" # black
# window
window = Tk()
window.title("Python Alarm Clock")
window.geometry('350x150')
window.configure(bg=bg_color)
# frame up
frame_line = Frame(window, width=400, height=5, bg=co1)
frame_line.grid(row=0, column=0)
frame_body = Frame(window, width=400, height=290, bg=bg_color)
frame_body.grid(row=1, column=0)
# configuring frame body
img = Image.open('icon.png')
img = img.resize((100, 100))
img = ImageTk.PhotoImage(img)
app_image = Label(frame_body, height=100, image=img, bg=bg_color)
app_image.place(x=10, y=10)
name = Label(frame_body, text="Alarm", font=('Ivy 18 bold'), bg=bg_color)
name.place(x=125, y=10)
# Hour
hour = Label(frame_body, text="hour", font=('Ivy 10 bold'), bg=bg_color, fg=co1)
hour.place(x=127, y=40)
c_hour = Combobox(frame_body, width=2, font=('arial 15'))
c_hour['values'] = ("00","01","02","03","04","05","06","07","08","09","10","11","12")
c_hour.current(0)
c_hour.place(x=130, y=58)
# Minute
min = Label(frame_body, text="min", font=('Ivy 10 bold'), bg=bg_color, fg=co1)
min.place(x=177, y=40)
c_min = Combobox(frame_body, width=2, font=('arial 15'))
c_min['values'] = tuple(f"{i:02}" for i in range(60))
c_min.current(0)
c_min.place(x=180, y=58)
# Second
sec = Label(frame_body, text="sec", font=('Ivy 10 bold'), bg=bg_color, fg=co1)
sec.place(x=227, y=40)
c_sec = Combobox(frame_body, width=2, font=('arial 15'))
c_sec['values'] = tuple(f"{i:02}" for i in range(60))
c_sec.current(0)
c_sec.place(x=230, y=58)
# Period (AM/PM)
period = Label(frame_body, text="period", font=('Ivy 10 bold'), bg=bg_color, fg=co1)
period.place(x=277, y=40)
c_period = Combobox(frame_body, width=3, font=('arial 15'))
c_period['values'] = ("AM", "PM")
c_period.current(0)
c_period.place(x=280, y=58)
# functions
def deactivate_alarm():
print('Deactivated alarm: ', selected.get())
mixer.music.stop()
def sound_alarm():
mixer.music.load('alarm2.mp3') # place your alarm sound file here
mixer.music.play()
selected.set(0)
rad2 = Radiobutton(frame_body, font=('arial 10 bold'), value=2,
text="Deactivate", bg=bg_color,
command=deactivate_alarm,
variable=selected)
rad2.place(x=187, y=95)
def alarm():
while True:
control = selected.get()
alarm_hour = c_hour.get()
alarm_minute = c_min.get()
alarm_sec = c_sec.get()
alarm_period = c_period.get().upper()
now = datetime.now()
hour = now.strftime("%I")
minute = now.strftime("%M")
second = now.strftime("%S")
period = now.strftime("%p")
if control == 1:
if (alarm_period == period and
alarm_hour == hour and
alarm_minute == minute and
alarm_sec == second):
print("Time to take a break!")
sound_alarm()
sleep(1)
def activate_alarm():
t = Thread(target=alarm)
t.start()
# radio button for activation
selected = IntVar()
rad1 = Radiobutton(frame_body, font=('arial 10 bold'), value=1,
text="Activate", bg=bg_color,
command=activate_alarm,
variable=selected)
rad1.place(x=125, y=95)
# initialize mixer
mixer.init()
# run tkinter loop
window.mainloop()
⚡ Before running this code:
-
Save an icon image as
icon.pngin the same folder. -
Save an alarm sound file (like
alarm2.mp3) in the same folder.
Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source
Article link: http://pybeginners.com/article/build-your-own-alarm-clock-in-python-with-tkinter/