Create Simple Analog Clock App Using Python

Hello Friends,

                       Today, In this post I am going to show you how we can easily create a simple analog clock app using python and Tkinter Module.     




                         

Introduction


We all are already aware of the importance of Clock in our day to day lifestyle. And Yes, This importance also inspired us to make any app related to Clock Topic so, that we can use it in our busy day to day lifestyle and This Script also going to help us in polishing our coding skills.
But Before Starting Today's Tutorial I Just want to notify you That Biforestinfo Blog is Best Blog for Python Programmers and there is nothing bad thing in following our blog on social. I hope you got my hints. hehehehe.

Friends, Maybe Some Of my python examples looks Very Hard For Beginners but Friends, Believe Me, My All Example Are Easy To Understand. They look Hard Because I Am Using OOP (Object Orient Programming). So, If You Want To Be Expert Then don't Lose Your Courage or guts. Now, let's move ahead and talk about the requirements to play with this script.


Requirements 


1. Python 2.x Or Python 3.x
2. Tkinter Module
3. Analog Clock BackGround Image
4. Basic Knowledge Of Tkinter and Python. 

How it's Going To Work


Actually, In this Script, we going to use Tkinter Canvas Widget. I am assuming that you already aware of Tkinter Canvas Widget. 

1. We have to create a root window.
2  create and pack canvas widget with root window. 
3. Use Create Image Function To Paste Background Image GIF Into Canvas Widget.
4. Use Canvas Line Function To Represent Analog Clock Sticks.
5. Use Time module to Continuously Update Analog Clock Sticks.

Done!

Looks Easy!. So, Let's Quickly Play With Real Codes Of Analog Clock Script. 

1. Analog.py



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Written By:
#  S.S.B
#  surajsinghbisht054@gmail.com
#  bitforestinfo.blogspot.com
#
# 
# Import Module
try:
 import Tkinter
except:
 import tkinter as Tkinter

import math # Required For Coordinates Calculation
import time # Required For Time Handling
#
#
# class
class main(Tkinter.Tk):
 def __init__(self):
  Tkinter.Tk.__init__(self)
  self.x=150 # Center Point x  
  self.y=150 # Center Point
  self.length=50 # Stick Length
  self.creating_all_function_trigger()

 # Creating Trigger For Other Functions
 def creating_all_function_trigger(self):
  self.create_canvas_for_shapes()
  self.creating_background_()
  self.creating_sticks()
  return

 # Creating Background
 def creating_background_(self):
  self.image=Tkinter.PhotoImage(file='clock.gif')
  self.canvas.create_image(150,150, image=self.image)
  return

 # creating Canvas
 def create_canvas_for_shapes(self):
  self.canvas=Tkinter.Canvas(self, bg='skyblue')
  self.canvas.pack(expand='yes',fill='both')
  return

 # Creating Moving Sticks
 def creating_sticks(self):
  self.sticks=[]
  for i in xrange(3):
   store=self.canvas.create_line(self.x, self.y,self.x+self.length,self.y+self.length,width=2)
   self.sticks.append(store)
  return

 # Function Need Regular Update
 def update_class(self):
  now=time.localtime()
  t = time.strptime(str(now.tm_hour), "%H")
  hour = int(time.strftime( "%I", t ))*5
  now=(hour,now.tm_min,now.tm_sec)
  # Changing Stick Coordinates
  for n,i in enumerate(now):
   x,y=self.canvas.coords(self.sticks[n])[0:2]
   cr=[x,y]
   cr.append(self.length*math.cos(math.radians(i*6)-math.radians(90))+self.x)
   cr.append(self.length*math.sin(math.radians(i*6)-math.radians(90))+self.y)
   self.canvas.coords(self.sticks[n], tuple(cr))
  return

# Main Function Trigger
if __name__ == '__main__':
 root=main()

 # Creating Main Loop
 while True:
  root.update()
  root.update_idletasks()
  root.update_class()

Now, To Run These Codes. Just Copy Above Codes Into A Script
And Run it Using Python.


If You like This Script And Tutorial And Wants To Try it Yourself Then You Can Download Raw script From My Github Account Repo.   Click here



I hope you Enjoyed This Tutorial.


For any Suggestion or help
post a  Comment below.

Share this

Related Posts

Previous
Next Post »