Create Alarm Clock App Using Python

Namaste friends,

                       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.

1. alarm.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Written By:
#  S.S.B
#  surajsinghbisht054@gmail.com
#  bitforestinfo.blogspot.com
#
# 
#
#
##################################################
######## Please Don't Remove Author Name #########
############### Thanks ###########################
##################################################
#
#
# Import Module
try:
 import Tkinter
 import ttk
except:
 import tkinter as Tkinter
 import tkinter.ttk as ttk
import datetime
import beep
import os

# Creating Clock Main Root Sub Class
class Clock(Tkinter.Tk):
 def __init__(self, *args, **kwargs):
  Tkinter.Tk.__init__(self, *args, **kwargs)
  self['padx']=20
  self['pady']=20
  # Creating Variables
  self.waiting_string_variable = Tkinter.IntVar()
  self.show_alarm_time = Tkinter.StringVar()
  self.show_alarm_time.set(datetime.datetime.now().ctime())
  self.alarm_delta_time = None
  self.create_first_label()
  self.create_second_box()

 # Creating Set Alarm time Showing Label
 def create_first_label(self):
  ttk.Label(self, textvariable=self.show_alarm_time, font = ("arial 20 bold")).grid(row=0, column=1, columnspan=2, padx=10,pady=10)
  return

 # Creating Keypad
 def create_second_box(self):
  ttk.Label(self, text="Wait For Seconds : ").grid(row=1, column=1, padx=10,pady=10)
  ttk.Entry(self, textvariable = self.waiting_string_variable).grid(row=1, column=2, padx=10,pady=10)
  ttk.Button(self, text="Exit", command=self.destroy).grid(row=3, column=1, padx=10,pady=10)
  ttk.Button(self, text="Set Alarm!", command=self.set_alarm_button).grid(row=3, column=2, padx=10,pady=10)
  return

 # 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 -*-
#
# Written By:
#  S.S.B
#  surajsinghbisht054@gmail.com
#  bitforestinfo.blogspot.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)


Done!

if you want to download latest raw examples then, 
check my GitHub Repository. Click Here

Or 

Check This Video



By SSB



Thanks For Reading

If You like my blog, then
don't forget to subscribe,
like, comment and share.
for more update,

Share this

Related Posts

Previous
Next Post »

2 comments

Write comments