Create an Alarm Clock With Python - Python Project

0
create an alarm clock with python

Learning Python programming language is easy with building something that matters to you. While you are learning things you should do some practice with the code that you are learning.

With Python Programming language you can create pretty much anything. When you have something in your hands to show it is a portfolio or maybe show it to your loved ones. It is really helpful in building a good profile.

Building an Alarm Clock With Python

In this project-based tutorial, I will guide you to create a simple alarm application using the Python programming language.

Prerequisites for This Project

Knowing some basics of Python can help you make this project. Though we will be using some of the basic modules of the Python Programming language. If you have some practice you might be familiar with these Python modules but do not worry, I am here to cover it all.

Before getting started, It will be good If you just have a look at these Python Packages for this Project. It will really help you make this project very easy to do the task for you.

Tkinter python module

Tkinter module is used for creating a graphical user interface

datatime python module

To make working with the DateTime objects easy we have used the datatime module.

winsound python module

We are creating an alarm application, so it is obvious that we will need to have some kind of sound in it. The python winsound module has covered them all.

threading module in Python

To run the application parallel to another task we have to run this app on a separate thread. We have used the python threading module to create a thread for this alarm application.

Getting Started with Coding!

Once you have gone through the above modules, now it will be very easy to do the task for you to create this alarm application with python.

Python Code For Creating an Alarm

Before discussing how code will work, let me just mention the complete code. This is the complete python code for creating an alarm clock. Below are some of the important methods that you should know about. I have also mentioned the code snippets from the complete code and then describe the working of the code.

from tkinter import *
import datetime
import time
import winsound
from threading import *
root = Tk()
root.geometry("400x200")
def Threading():
	t1=Thread(target=alarm)
	t1.start()

def alarm():
	while True:
		set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
		time.sleep(1)
		current_time = datetime.datetime.now().strftime("%H:%M:%S")
		print(current_time,set_alarm_time)
		if current_time == set_alarm_time:
			print("Time to Wake up")
			winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
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 = list(range(0,25))
hourszf=[]
for n in hours:
    hourszf.append((str(n).zfill(2)))
hours = tuple(hourszf)

hour.set(hours[0])

hrs = OptionMenu(frame, hour, *hours)
hrs.pack(side=LEFT)

minute = StringVar(root)
minutes = list(range(0,61))
minuteszf=[]
for m in minutes:
    minuteszf.append((str(m).zfill(2)))
minutes = tuple(minuteszf)
minute.set(minutes[0])

mins = OptionMenu(frame, minute, *minutes)
mins.pack(side=LEFT)

second = StringVar(root)
seconds = list(range(0,61))
secondszf=[]
for m in seconds:
    secondszf.append((str(m).zfill(2)))
seconds = tuple(secondszf)
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)
root.mainloop()


Link to the code

By following this link you will see my gist hosting this alarm app script. You can follow me on GitHub for more awesome projects.

Code Snippets used in Alarm Clock

These are the good snippet from the project above that can be helpful to you.

Importing modules

Following are the modules used in this project. The import statement is used to import it in the script.

from tkinter import *
import datetime
import time
import winsound


Creating the Tkinter Window

The TK class is used from the Tkinter module to create a root for creating the window. The width of the window is 400px and the height is 200px. A thread function is defined as will that runs the alarm function is a separate thread.

root = Tk()
root.geometry("400x200")
def Threading():
	t1=Thread(target=alarm)
	t1.start()


Creating the Alarm function

This Function is responsible for the logic of the Alarm. The function will play the sound once it matches the time being set.

def alarm():
	while True:
		set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
		time.sleep(1)
		current_time = datetime.datetime.now().strftime("%H:%M:%S")
		print(current_time,set_alarm_time)
		if current_time == set_alarm_time:
			print("Time to Wake up")
			winsound.PlaySound("sound.wav",winsound.SND_ASYNC)


Creating the Range of the Hours for Alarm

We have created a tuple of hours from 0 to 24. We have saved them as double-digit. For example, the '1' will be looking like '01' as this is only for aesthetics. We are filling the digit by 0.

hourszf=[]
for n in hours:
    hourszf.append((str(n).zfill(2)))
hours = tuple(hourszf)


Creating the Range of Minutes

The number of minutes in one hour is 60, so we are creating a range of digits from 0 to 60. The same is like hours, we are also padding the single-digit number with 0's as we want them to look good visually.

minutes = list(range(0,61))
minuteszf=[]
for m in minutes:
    minuteszf.append((str(m).zfill(2)))
minutes = tuple(minuteszf)
minute.set(minutes[0])


Create a Range of Seconds

Same as the Hours and Minutes, we have also used the same logic to create a range of 60 seconds. Nothing is different except the name of the variables, though it is not a good approach.

seconds = list(range(0,61))
secondszf=[]
for m in seconds:
    secondszf.append((str(m).zfill(2)))
seconds = tuple(secondszf)
second.set(seconds[0])



Summary and Conclusion

I hope you have understood the complete code, This was an easy-to-follow tutorial on how to create an alarm clock with Python. If you still have any questions or you just want to talk to me regarding the code or something personal to ask. Please let me know on my WhatsApp +923159675198.

    See more :
  1. How to use Python OS Module?
  2. Python Libraries for video Editing
  3. Python Libraries for Downloading media

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !