Posted by
Suraj Singh
on April 06, 2017 ·
10 mins read
hii readers,
Today, In this tutorial I'm' going to show you how we can create a simple alarm clock app using python and Tkinter?.
Introduction
before starting our tutorial, Above I just pasted my screenshot of Simple Alarm Clock App. I know it's not looking that much good but yeah, quite works fine and also easy to code. And Yes, if you are a python beginner programmer then, I will suggest you to keep in your mind that Bitforestinfo is the right place for you because to be an expert, we have to start from basic tools and yes, alarm clock also comes in basic app.
As You Know, the Alarm clock is a very useful tool and used by millions of peoples for different purposes. Therefore today we will simply try to create an alarm clock app for our desktop python script language. basically, this app is just for fun and for practice also and of course, you can modify these codes as your requirement and can also comment your modified codes into comment box to share with our targeted audience.
How it's Going To Work.
Actually, In this app, we are just playing with high-level Interface. I mean, for Beep Voice We going to use Linux Command line Tool called beep that will help us to produce beep noise from hardware to act as alarm clock ringtone. Tkinter For Graphical Support, Time module for playing with actual timing and done! hahaha I know it's very easy to make. basically, we just going to create a simple graphical app that will take alarm time from the user, wait until the time arrives and boom, Just trigger beep noise command tool.
quite Easy.
Requirements
I am using Ubuntu and Python Version 2.7
Example Codes.
Now, let me explain you, how we will create this app?. Basically, In First Step we going to create 2 scripts in the same directory one for handing time manipulation, Graphical Window And Other Function, Another Script is for calling Beep Command-line Tool.
yet not understand!
Brief Explanation
Going To Create 2 Scripts
1. alarm.py
2. beep.py
here, alarm.py script is responsible for graphics and time-related function and our another script, beep.py is created for handling beep sound generated by computer hardware.
For calculating and comparing time, we will use python DateTime module. ( if you don't know, how to use python DateTime module.. click here )
So, let's play with some practical codes. here, for easy understanding, I am using lots of comments in codes read carefully and try to understand all codes.
# Set Alarm Function def set_alarm_button(self): try: sec = self.waiting_string_variable.get() self.alarm_delta_time = datetime.datetime.now()+datetime.timedelta(seconds=sec) self.show_alarm_time.set(self.alarm_delta_time.ctime().__str__()) except: self.waiting_string_variable.set(0) return
# Updating Loop def regular_update(self): self.update() self.update_idletasks() if self.alarm_delta_time: if datetime.datetime.now() > self.alarm_delta_time: beep.beep(5)
return
# Main Function def main(): root = Clock(className = " Simple Alram Clock By Bitforestinfo") while True: root.regular_update() return
# Main trigger Function if __name__ == '__main__': main()
2. beep.py
#!/usr/bin/python # -*- coding: utf-8 -*- # # # Suraj # surajsinghbisht054@gmail.com # www.bitforestinfo.com # # # # ################################################## ######## Please Don't Remove Author Name ######### ############### Thanks ########################### ################################################## # # # Import Module import os # # Here, I am Using Ubuntu # that's why current function is working for me # but if you are using any other operating system # then, you need to customize below function as # your operating system supports. # # Check This Links # # http://askubuntu.com/questions/19906/beep-in-shell-script-not-working # http://stackoverflow.com/questions/6537481/python-making-a-beep-noise # http://stackoverflow.com/questions/16573051/python-sound-alarm-when-code-finishes # # # Beep Creating Function def beep(sec): for i in range(sec): os.system("beep -f 555 -l 460") return
# Trigger Function if __name__ == '__main__': beep(5)