Once the dial is ready with all markings ( read part 1 , creating dial ), to this we will add needles to the clock.

Following points may be noted.

- Each segment is 6 degree and there are 60 segments in our dial.
- Each needle has to increase by 6 degree to cross one segment.
- For each 360 degree rotation of second needle, minute needle will move by one segment ( 6 degree ).
- Hour needle moves 30 degree or 5 segments of 6 degree each on every completion of one rotation of 360 degree by minutes needle.
- Zero degree is the 12 O’clock position.

Minute needle has to increase by one segment of 6 degree on each call to my_minute() function. ( on every 60 seconds).

Hour needle has to increase by 0.5 degree on each call to my_hour() function.

```
in_degree = 0 # for creating dial markings
in_degree_s=int(time.strftime('%S')) *6 # local second
in_degree_m=int(time.strftime('%M'))*6 # local minutes
in_degree_h=int(time.strftime('%I')) * 30 # 12 hour format
print(in_degree_h)
```

```
in_radian = math.radians(in_degree_s)
x2=x+rs*math.sin(in_radian)
y2=y-rs*math.cos(in_radian)
second=c1.create_line(x,y,x2,y2,fill='red',width=2) # draw the needle
```

Above code will draw the second needle based on the local time second value and the variable `c1.after(1000,my_second)`

Each call to this function will move the second needle by 6 degree (` in_degree_s=in_degree_s+6 `

)
The movement of second needle is achieved by deleting the second needle and redrawing it at the new coordinates.
```
c1.delete(second) # delete the needle
x2=x+rs*math.sin(in_radian) # Horizontal coordinate of outer edge
y2=y-rs*math.cos(in_radian) # vertical coordinate of outer adge
second=c1.create_line(x,y,x2,y2,arrow='last',fill='red',width=2)
```

Full code for movement of Second needle is here .
```
def my_second():
global in_degree_s,second
in_radian = math.radians(in_degree_s) # from degree to ra
c1.delete(second) # delete the needle
x2=x+rs*math.sin(in_radian) # Horizontal coordinate of outer edge
y2=y-rs*math.cos(in_radian) # vertical coordinate of outer adge
second=c1.create_line(x,y,x2,y2,arrow='last',fill='red',width=2)
if(in_degree_s>=360): # one rotattion is over if reached 360
in_degree_s=0 # start from zero angle again
my_minute() # call the minute needle to move one segment.
in_degree_s=in_degree_s+6 # increment of one segment is 6 degree
c1.after(1000,my_second) # recrusive call after 1000 milliseconds
```

```
if(in_degree_s>=360): # one rotattion is over if reached 360
in_degree_s=0 # start from zero angle again
my_minute() # call the minute needle to move one segment.
c1.after(1000,my_second) # recrusive call after 1000 milliseconds
```

```
in_radian = math.radians(in_degree_m)
x2=x+rm*math.sin(in_radian)
y2=y-rm*math.cos(in_radian)
minute=c1.create_line(x,y,x2,y2,width=4,fill='green')
```

Full code for Minute needle is here .
```
def my_minute():
global in_degree_m,minute
in_degree_m=in_degree_m+6 # increment for each segment
in_radian = math.radians(in_degree_m) # coverting to radian
c1.delete(minute) # delete the previous needle
x2=x+rm*math.sin(in_radian) # Horizontal coordinate of outer edge
y2=y-rm*math.cos(in_radian) # vertical coordinate of outer dege
minute=c1.create_line(x,y,x2,y2,width=4,fill='green')
my_hour() # calling hour needle to move
if(in_degree_m>=360): # One rotation of 360 degree is over
in_degree_m=0
```

Note that for 360 degree rotation of minute ( one hour ) the hour needle moves by 30 degree. To get the initial angle for hour needle we have to multiply minutes angle value like this. Here 30/360 = 0.0833333

```
in_degree_h=in_degree_h+(in_degree_m*0.0833333)
in_radian = math.radians(in_degree_h)
x2=x+rh*math.sin(in_radian)
y2=y-rh*math.cos(in_radian)
hour=c1.create_line(x,y,x2,y2,width=6,fill='#a83e32')
```

The code for hour needle is here
```
def my_hour():
global in_degree_h,hour
in_degree_h=in_degree_h+0.5 # increment in each step
in_radian = math.radians(in_degree_h) # in radian
c1.delete(hour) # deleting hour needle
x2=x+rh*math.sin(in_radian) # Horizontal coordinate for outer edge
y2=y-rh*math.cos(in_radian) # vertical coordinate for outer adge
hour=c1.create_line(x,y,x2,y2,width=6,fill='#a83e32')
if(in_degree_h>=360):
in_degree_h=0
```

From the main script we have to call ```
my_second() # calling to start
my_w.mainloop()
```

variables | Details |
---|---|

rs,rm,rh: | Length of Second needle,minute needle and hour needle |

x,y | Center coordinates. |

x2,y2 | Outer edge coordinates of three needles . |

in_degree | Angle in degree. Each segment is 6 degree |

in_radian | Inputs to sin() and cos() functions value in radian. Value in degree is converted to radian before using with sin() or cos() functions. |

Python Tkinter Projects

Tkinter Text Tkinter Entry How to Validate user entered data

This article is written by **plus2net.com** team.
https://www.plus2net.com

* indicates required

Post your comments , suggestion , error , requirements etc here

Python Video Tutorials

Python SQLite Video Tutorials

Python MySQL Video Tutorials

Python Tkinter Video Tutorials