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 in_degree_s will hold the degree based on this local time value.
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) # recursive 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) # recursive 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) # converting to radians
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
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() once to start the clock.
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. |
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.