Introduction
In this tutorial, we’ll build a Python Alarm Clock that can be used for everything from waking you up in the morning to notifying you at a specified time. The project will allow users to choose a time for their alarm to sound from a menu and automatically notify them at the appropriate moment. Whether you’re new to Python or have some past expertise, this project is an excellent way to learn more about creating functional applications and put your skills to use in a useful and enjoyable way.
Four key steps to make Python Alarm Clock:
Step 1: Importing the Necessary Libraries
Importing all of the necessary libraries and modules is the initial step in starting the project. The datetime, time, and mixer modules from pygames will produce the alarm and sound effects, while the Tkinter library will be used for the visuals. You must first establish the Tkinter window and import the necessary libraries. As an example, let’s type:
Download The Audio file From here: violet-cat-415996.hostingersite.com
# Importing all the necessary modules
from time import strftime
from tkinter import *
import time
import datetime
from pygame import mixer
Step 2: Creating Tkinter GUI
# Setting the main screen
root = Tk()
root.title("CopyAssignment.com")
root.geometry("265x270")
root.resizable(width=False,height=False)
After all of the modules have been imported, it’s time to start Tkinter and design the program’s graphical user interface. We’ll begin by launching Tkinter and creating a window for the Python Alarm Clock. In this case, we set the window size to 265×270 and the window resizability to false.
Step 3: Create the menus, buttons, and labels for the GUI
Now that the GUI has been established, it is time to begin adding frames, labels, and buttons to the GUI. To begin, we’ll make two essential labels: “Alarm Clock” and “Set Time:”. These labels, which will be centered in the window, will inform users about the software and how to interact with it. Then, we’ll create a widget that uses a StringVar to hold all of the hour values that users may select from. Because the clock operates on a 24-hour basis, the readings will range from 0 to 24.
# Creating and placing all the components of the window
hrs=StringVar()
mins=StringVar()
secs=StringVar()
Label(root, font = ('arial', 16, 'bold'),
text="Set Your Alarm Clock!\n [ Its 24Hrs Clock ]\n").grid(row=1,columnspan=4)
Label(root, font = ('arial', 11, 'bold'),
text="Hour").grid(row=3,column=1)
hrbtn=Entry(root,textvariable=hrs,width=5,
font =('arial', 18, 'bold')).grid(row=4,column=1)
Label(root, font = ('arial', 11, 'bold'),
text="Minute").grid(row=3,column=2)
minbtn=Entry(root,textvariable=mins,width=5,
font = ('arial', 18, 'bold')).grid(row=4,column=2)
Label(root, font = ('arial', 11, 'bold'),
text="Second").grid(row=3,column=3)
secbtn=Entry(root,textvariable=secs,
width=5,font = ('arial', 18, 'bold')).grid(row=4,column=3)
Label(root, font = ('arial', 10, 'bold'),
text="Welcome At The CopyAssignment.com").grid(row=7,columnspan=6)
We’ll also set the default hour value to 0 until the user changes it. We’ll then create two additional widgets, one for minutes and one for seconds. These will also be formatted with Tkinter in the same manner as the hours.
Finally, we’ll add a “set alarm” button with the following code:
setbtn=Button(root,text="Set Timer!",command=setalarm,bg="#D4AC0D",
fg="Black",font = ('arial', 19, 'bold')).grid(row=8,columnspan=4)
timeleft = Label(root,font=('arial', 20, 'bold'))
timeleft.grid()
Step 4: Tuning the alarm Function!
Now that the GUI is finished, it’s time to build the alarm function in Python Alarm Clock. Begin by creating a function named “alarmclock” and then a while statement. This statement will be used to receive input from the hour, minute, and second widgets, and then a sleep command will be used to make the program wait for one second. Where Setalarm Function: It generates an alarm by using the alarmClock method and supplying the alarm time as a parameter (if the user has entered a correct and non-empty time).
def setalarm():
alarmtime=f"{hrs.get()}:{mins.get()}:{secs.get()}"
print(alarmtime)
if(alarmtime!="::"):
alarmclock(alarmtime)
def alarmclock(alarmtime):
while True:
time.sleep(1)
# Getting current time by using .striftime() method of the datetime module's datetime file's now function
time_now=datetime.datetime.now().strftime("%H:%M:%S")
print(time_now)
if time_now==alarmtime:
Wakeup=Label(root, font = ('arial', 13, 'bold'),
text="Wake up!Wake up!Wake up",fg="Black").grid(row=9,columnspan=4)
print("wake up!")
mixer.init()
# Playing a sound when the current time is the same as the alarm time
mixer.music.load(r'loud-alarm-02-46010.mp3')
mixer.music.play()
break
Next, we’ll add a piece of code that compares the computer’s local time to the specified alarm time. The datetime module will be used for this. There are now only a few lines left to code! At this point, we just need an if-statement to verify if the times are the same, as well as a command to run Tkinter. Begin by writing an if-statement that checks to see if the current time is equal to the alarm time. If this is the case, we want to print “wake up time” and play the Python Alarm Clock sound. Finally, we’ll add a command to run the Tkinter code, and we’ll be done! Now that the project is completed,
Complete Source Code of Python Alarm Clock | Tkinter GUI App
# Importing all the necessary modules from time import strftime from tkinter import * import time import datetime from pygame import mixer # Setting the main screen root = Tk() root.title("CopyAssignment.com") root.geometry("265x270") root.resizable(width=False,height=False) def setalarm(): alarmtime=f"{hrs.get()}:{mins.get()}:{secs.get()}" print(alarmtime) if(alarmtime!="::"): alarmclock(alarmtime) def alarmclock(alarmtime): while True: time.sleep(1) # Getting current time by using .striftime() method of the datetime module's datetime file's now function time_now=datetime.datetime.now().strftime("%H:%M:%S") print(time_now) if time_now==alarmtime: Wakeup=Label(root, font = ('arial', 13, 'bold'), text="Wake up!Wake up!Wake up",fg="Black").grid(row=9,columnspan=4) print("wake up!") mixer.init() # Playing a sound when the current time is the same as the alarm time mixer.music.load(r'loud-alarm-02-46010.mp3') mixer.music.play() break # Creating and placing all the components of the window hrs=StringVar() mins=StringVar() secs=StringVar() Label(root, font = ('arial', 16, 'bold'), text="Set Your Alarm Clock!\n [ Its 24Hrs Clock ]\n").grid(row=1,columnspan=4) Label(root, font = ('arial', 11, 'bold'), text="Hour").grid(row=3,column=1) hrbtn=Entry(root,textvariable=hrs,width=5, font =('arial', 18, 'bold')).grid(row=4,column=1) Label(root, font = ('arial', 11, 'bold'), text="Minute").grid(row=3,column=2) minbtn=Entry(root,textvariable=mins,width=5, font = ('arial', 18, 'bold')).grid(row=4,column=2) Label(root, font = ('arial', 11, 'bold'), text="Second").grid(row=3,column=3) secbtn=Entry(root,textvariable=secs, width=5,font = ('arial', 18, 'bold')).grid(row=4,column=3) Label(root, font = ('arial', 10, 'bold'), text="Welcome At The CopyAssignment.com").grid(row=7,columnspan=6) setbtn=Button(root,text="Set Timer!",command=setalarm,bg="#D4AC0D", fg="Black",font = ('arial', 19, 'bold')).grid(row=8,columnspan=4) timeleft = Label(root,font=('arial', 20, 'bold')) timeleft.grid() mainloop()
The output of Python Alarm Clock
The project of Python Alarm Clock is finished! You may now use this alarm clock to notify you of events at specified times or to wake you up after a snooze. Feel free to run your code right now to check how it works. If you get stuck or have problems with your code, Please comment to us below.
Also Read:
- Imagination to Reality, Unlocking the Future: Genesis Physics Engine for 4D Simulation
- Simple Code to compare Speed of Python, Java, and C++?
- Falling Stars Animation on Python.Hub October 2024
- Most Underrated Database Trick | Life-Saving SQL Command
- Python List Methods
- Top 5 Free HTML Resume Templates in 2024 | With Source Code
- How to See Connected Wi-Fi Passwords in Windows?
- 2023 Merry Christmas using Python Turtle
- 23 AI Tools You Won’t Believe are Free
- Python 3.12.1 is Now Available
- Best Deepfake Apps and Websites You Can Try for Fun
- Amazon launched free Prompt Engineering course: Enroll Now
- 10 GitHub Repositories to Master Machine Learning
- Hello World in 35 Programming Languages
- How to Scrape Data From Any Website with Python?
- Become Job Ready With Free Harvard Computer Science course: Enroll Now
- Free Python Certification course from Alison: Good for Resume
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- Udacity Giving Free Python Course: Here is how to Enroll
- Love Babbar’s Income Revealed
- Top 5 Websites to Learn Programming in 2024
- Python Internship for college students and freshers: Apply Here
- Microsoft Giving Free Python Course in 2023: Enroll Now
- Top 5 Free Python Courses on YouTube in 2024
- Complete Python Roadmap for Beginners in 2024
- New secrets to Earn money with Python in 2024
- Connect with HR Directly – Job Hack
- Google offering free Python course: Enroll Today
- What is an AI Tool?
- Google Internship 2024