Create an Alarm Clock in Python with GUI

There is no other way to learn something than to implement it practically. So we are here with another interesting project on python.

This time, you will learn how to create a simple alarm clock with python and Tkinter.

Python alarm clock is a gui application, which can be used on a computer to set an alarm. Although it’s a simple project for beginners, but it’s interesting to implement various functionalities.

Prerequisites

The prerequisites are: basic concepts of Python and Tkinter

To install the libraries, you can use pip installer from the 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

Follow TechVidvan on Google & Stay updated with latest technology trends

Creating main.py

from time import strftime 
from tkinter import * 

import time
import datetime
from pygame import mixer
 
root = Tk() 
root.title('TechVidvan 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() 

Functions Used

1. Setalarm: It sets an alarm by calling alarmClock method by passing the alarm time as argument(if the user has entered a correct and non-empty time).

2. alarmclock: This is the most important method because it performs the following tasks:

a. It stores the current time in time_now in specified format (“%H:%M:%S”)

b. It also checks if current time matches the alarm time. As soon as time matches it displays a wake-up message and plays the alarm song using pygame and mixer. And if doesn’t match the time_now it continues step b after sleeping for one second.

Variables Used:

1. root: the main GUI window.

2. Hrs, Mins, Secs: They are tkinter string variables that store the hour’s, minute’s and second’s value entered by the user before setting an alarm.

3. Greet: It is a label to display the message” Take a short nap!”.

4. We also have hrbtn, minbtn , secbtn to take respective values from the user.

Python Alarm Clock Project Output

alarm clock 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.

Your opinion matters
Please write your valuable feedback about TechVidvan on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *