DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Make Simple Alarm in Python
EjinJS
EjinJS

Posted on

Make Simple Alarm in Python

This is a very simple post and i give you guys some more tips about libraries to expand your alarm application,if you have knowledge about any python GUI , You can easily convert this into a GUI application

Always the first things will be installing requirements
As i said this is a simple alarm so we choose simple libraries for this

At,First install a Library called playsound , to play the sound
the next thing is datetime module , which is built in python module,so there is no need to install it

Make a new python file , name the file any name you like!!
Import the modules
from playsound import playsound
from datetime import datetime

Make a function called alarm()
Alt Text

put current_state = False , which means now alarm is not ringing or the time to ring hasnt reached yet...

make a varieable and set the time you want to ring , Note write the time in String , also remember datetime module is in 24hr Format
Alt Text

Now make a while loop Like this
Alt Text

I explain what is happening here this loops works while the current state is at False , next we set a localtime , datetime.now() returns us current date and time , we strip the hour and minutes using strftime('%H:%M')
Now we make an IF statement
if the localtime is equal to the alarm time(which we set at the first) , it will print some text and with the playsound module it will play a music or something when the if statement is correct
Alt Text

type the path of that music in your computer as parameter for playsound function

That's it hope you liked this post
make sure to rate this post if alarm worked well
feel free to comment in the post
Have great day Little Programmers!!

Top comments (0)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›