Create an Alarm Clock in Python

Alarm Clock

There is no other way to learn something than to put it into practice. So here we are with another exciting project on python. This time, you will learn how to create a simple alarm clock with python and Tkinter.

The python alarm clock is a gui program, which can be used on a computer to set an alarm. Although it is a simple project for beginners, it is interesting to use a variety of activities.

Prerequisites

What is needed are: the basic concepts of Python and Tkinter

To install libraries, you can use the pip installer from cmd / Terminal:

pip install tkinter

Download Python Alarm Clock Code

Before proceeding ahead, please download source code of python alarm clock: Python Alarm Clock Code

Creating main.py
from time import strftime 
from tkinter import * 

import time
import datetime
from pygame import mixer
 
root = Tk() 
root.title('Geeky Codes Alarm-Clock') 

def setalarm():
    alarmtime=f"{hrs.get()}:{mins.get()}:{secs.get()}"
    print(alarmtime)
    if(alarmtime!="::"):
        alarmclock(alarmtime) 

def alarmclock(alarmtime): 
    while True:
        time.sleep(1)
        time_now=datetime.datetime.now().strftime("%H:%M:%S")
        print(time_now)
        if time_now==alarmtime:
            Wakeup=Label(root, font = ('arial', 20, 'bold'),
            text="Wake up!Wake up!Wake up",bg="DodgerBlue2",fg="white").grid(row=6,columnspan=3)
            print("wake up!")
            mixer.init()
            mixer.music.load(r'C:\Users\BOSS\Desktop\MyPlaylist\WakeUP.mp3')
            mixer.music.play()
            break


hrs=StringVar()
mins=StringVar()
secs=StringVar()

greet=Label(root, font = ('arial', 20, 'bold'),
text="Take a short nap!").grid(row=1,columnspan=3)

hrbtn=Entry(root,textvariable=hrs,width=5,font =('arial', 20, 'bold'))
hrbtn.grid(row=2,column=1)

minbtn=Entry(root,textvariable=mins,
width=5,font = ('arial', 20, 'bold')).grid(row=2,column=2)

secbtn=Entry(root,textvariable=secs,
width=5,font = ('arial', 20, 'bold')).grid(row=2,column=3)

setbtn=Button(root,text="set alarm",command=setalarm,bg="DodgerBlue2",
fg="white",font = ('arial', 20, 'bold')).grid(row=4,columnspan=3)

timeleft = Label(root,font=('arial', 20, 'bold')) 
timeleft.grid()
  
mainloop() 

Used Functions

  1. Setalarm: Sets the alarm by calling the alarm method Clock by passing the alarm time as an argument (if the user has set the correct and incorrect time).
  2. Alarm clock: This is a very important method because it performs the following functions:

a. Saves the current time to time_now in a specific format (“% H:% M:% S”)

b. It also checks that the current time matches the alarm time. As soon as time goes by it shows a wake-up message and plays an alarm song using a pygame and mixer. And if it doesn’t keep up with the time_now it goes on step b after sleeping for a second.

Variables used:

  1. root: main GUI window.
  2. Hrs, Mins, Secs: It is a flexible tkinter cable that keeps track of the hour, minute and second set by the user before setting the alarm.
  3. Greetings: It is a label that conveys the message “Take a short nap!”.
  4. We also have hrbtn, minbtn, secbtn to take different values ​​from the user.

Python Alarm Clock Project Output

Summary

We have successfully created alarm clock project in python. This is an interesting python project for beginners where we used tkinter and pygame.

Happy Learning 🙂

Important Notice for college students

If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

For more Programming related blogs Visit Us Geekycodes . Follow us on Instagram.

By geekycodesco

Leave a Reply

%d bloggers like this: