How To Create Analog Clock Using Python And Tkinter

Hello Friends,
                           Today, We will Discuss About How we can Create Analog Clock Using Python And Tkinter. Friends, I Know My Some Python Examples Are looks like Very Hard For Beginners but Friends, Believe Me My All Example Are Easy For Understand. They looks Hard Because I Am Using OOP (Object Orient Programming). So, If Want To Be Expert Then don't Lose Your Courage or guts.

So, Let's Talk About This Example

But First, Screen Shot



Now, Examples Code:



 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()

For Downloading This Example Click here

thanks for visiting my blog.


For any question or Suggestion or help
S.S.B
Email:  surajsinghbisht054@gmail.com
Or post Comment.
please feel free to contact me.

Share this

Related Posts

Previous
Next Post »