Signup/Sign In
PUBLISHED ON: FEBRUARY 25, 2022

Alarm Clock Using Python Language

Alarm Clock Using Python

The Alarm Clock Using Python is a project built in a python programming language, The purpose of this Python Alarm Clock Tutorial is to create a Python Alarm Clock GUI.

A Python Alarm Clock Script includes essential libraries such as DateTime and Tkinter, which assist us in constructing projects utilizing the current date and time. They also give a user interface to set the alarm according to the demand in a 24-hour format. To start developing this primary project, Alarm Clock Using Python, make sure that you have Pycharm IDE installed on your computer.

Steps on How to Make An Alarm Clock Using Python.

Alarm Clock Using Python With Source Code

Step 1: Create a project name.

First, open Pycharm IDE and then create a “project name” after generating a project name, click the “create” button.

Step 2: Create a python file.

Second, after establishing a project name, “right-click” your project name and then choose “new” after that, click the “python file“.

Step 3: Name your python file.

Third, after generating a python file, Name your python file; click “enter“.

Step 4: The actual code.

You can copy the code supplied below and download the entire source code below.

Code For Importing Modules

from tkinter import *
import datetime
import time
import winsound

While the following code imports every module that is being called in running the application.

Code For The Module Actual Time

def actual_time():
    set_alarm_timer = f"{hour.get()}:{min.get()}:{sec.get()}"
    alarm(set_alarm_timer)

This module is the real or the current time that is being called while setting the alarm.

Code For The Module Of Setting The Alarm

def alarm():
    # Infinite Loop
    while True:
        # Set Alarm
        set_alarm = f"{hour.get()}:{minute.get()}:{second.get()}"
 
        # Wait for one seconds
        time.sleep(1)
 
        # Get current time
        current_time = datetime.datetime.now().strftime("%H:%M:%S")
 
        # Check whether set alarm is equal to current time or not
        if current_time == set_alarm:
            print("Time to Wake up")
            # Playing sound
            winsound.PlaySound("sound.wav",winsound.SND_ASYNC)

In this module which is the setup of alarm that is performed.

Code For The GUI

clock = Tk()
clock.title("DataFlair Alarm Clock")
clock.geometry("400x200")
time_format=Label(clock, text= "Enter time in 24 hour format!", fg="red",bg="black",font="Arial").place(x=60,y=120)
addTime = Label(clock,text = "Hour  Min   Sec",font=60).place(x = 110)
setYourAlarm = Label(clock,text = "When to wake you up",fg="blue",relief = "solid",font=("Helevetica",7,"bold")).place(x=0, y=29)
 
# The Variables we require to set the alarm(initialization):
hour = StringVar()
min = StringVar()
sec = StringVar()
 
#Time required to set the alarm clock:
hourTime= Entry(clock,textvariable = hour,bg = "pink",width = 15).place(x=110,y=30)
minTime= Entry(clock,textvariable = min,bg = "pink",width = 15).place(x=150,y=30)
secTime = Entry(clock,textvariable = sec,bg = "pink",width = 15).place(x=200,y=30)
 
#To take the time input by user:
submit = Button(clock,text = "Set Alarm",fg="red",width = 10,command = actual_time).place(x =110,y=70)
 
clock.mainloop()
#Execution of the window.

This module is the design or the graphical user interface or (GUI) of this project.

Complete Source Code of Alarm Clock Using Python

# Import Required Library
from tkinter import *
import datetime
import time
import winsound
from threading import *
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x200")
 
# Use Threading
def Threading():
    t1=Thread(target=alarm)
    t1.start()
 
def alarm():
    # Infinite Loop
    while True:
        # Set Alarm
        set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
 
        # Wait for one seconds
        time.sleep(1)
 
        # Get current time
        current_time = datetime.datetime.now().strftime("%H:%M:%S")
        print(current_time,set_alarm_time)
 
        # Check whether set alarm is equal to current time or not
        if current_time == set_alarm_time:
            print("Time to Wake up")
            # Playing sound
            winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
 
# Add Labels, Frame, Button, Optionmenus
Label(root,text="Alarm Clock",font=("Helvetica 20 bold"),fg="red").pack(pady=10)
Label(root,text="Set Time",font=("Helvetica 15 bold")).pack()
 
frame = Frame(root)
frame.pack()
 
hour = StringVar(root)
hours = ('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'
        )
hour.set(hours[0])
 
hrs = OptionMenu(frame, hour, *hours)
hrs.pack(side=LEFT)
 
minute = StringVar(root)
minutes = ('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', '60')
minute.set(minutes[0])
 
mins = OptionMenu(frame, minute, *minutes)
mins.pack(side=LEFT)
 
second = StringVar(root)
seconds = ('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', '60')
second.set(seconds[0])
 
secs = OptionMenu(frame, second, *seconds)
secs.pack(side=LEFT)
 
Button(root,text="Set Alarm",font=("Helvetica 15"),command=Threading).pack(pady=20)
 
# Execute Tkinter
root.mainloop()

Output:

Alarm

Alrm

Conclusion

The goal of this project, Alarm Clock Using Python, is to create an alarm clock using Python. Python contains some essential libraries such as DateTime and Tkinter, which assist us in constructing a project utilizing the current date and time. They also give a user interface to set the alarm according to the demand in a 24-hour format.



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.