how to create simple alarm clock using python and tkinter

Namaste friends,

                       Today, I am going to show you how to create a simple alarm clock using python and Tkinter?.


if you are a python beginner programmer. then, this is the right place for you because to be an expert, we need to start from basic tools.

As You Know, the Alarm clock is a very useful tool and used by millions of peoples for different purposes. so, today we will also try to create an alarm clock app for our desktop.
and for these python codes.

here,

I am using Ubuntu and Python Version 2.7

Now, let me explain you, how we will create this app?.

first,
           we will create 2 scripts in the same directory. because we are going to divide our app functions into 2 scripts.

1. alarm.py


2. beep.py


here, alarm.py script is responsible for app graphics and time-related function. and beep.py is created for beep sound.
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)

if you want to download latest raw examples.

then, check my GitHub Repository. Click Here


Thanks For Reading

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


visit my blog. regularly.


Share this

Related Posts

Previous
Next Post »

2 comments

Write comments