How To Create Analog Clock in Python
In this article, we are going to learn how to create an Analog Clock in Python.
Requirements:
- Python
- Python Turtle Module
- Basic Knowlege of Python and Turtle
Creating Analog Clock in Python
First, we need to import the clock image from turtle graphics module:
import turtle import time wndw = turtle.Screen() wndw.bgcolor("black") wndw.setup(width=600, height=600) wndw.title("Analogue Clock") wndw.tracer(0)
Then we have to draw the clock and define the hour, minutes and seconds of the clock:
def draw_clock(hr, mn, sec, pen): # Draw clock face pen.up() pen.goto(0, 210) pen.setheading(180) pen.color("green") pen.pendown() pen.circle(210) # Draw hour hashes pen.up() pen.goto(0, 0) pen.setheading(90) for _ in range(12): pen.fd(190) pen.pendown() pen.fd(20) pen.penup() pen.goto(0, 0) pen.rt(30)
Drawing the hand of the clock and adding the degree is the third step :
# Draw the hands # Each tuple in list hands describes the color, the length # and the divisor for the angle hands = [("white", 80, 12), ("blue", 150, 60), ("red", 110, 60)] time_set = (hr, mn, sec) for hand in hands: time_part = time_set[hands.index(hand)] angle = (time_part/hand[2])*360 pen.penup() pen.goto(0, 0) pen.color(hand[0]) pen.setheading(90) pen.rt(angle) pen.pendown() pen.fd(hand[1])
After that, we have to add a while True statement. A while True means a loop forever
while True: hr = int(time.strftime("%I")) mn = int(time.strftime("%M")) sec = int(time.strftime("%S")) draw_clock(hr, mn, sec, pen) wndw.update() time.sleep(1) pen.clear() wndw.mainloop()
Code :
import turtle import time wndw = turtle.Screen() wndw.bgcolor("black") wndw.setup(width=600, height=600) wndw.title("Analogue Clock") wndw.tracer(0) # Create the drawing pen pen = turtle.Turtle() pen.hideturtle() pen.speed(0) pen.pensize(3) def draw_clock(hr, mn, sec, pen): # Draw clock face pen.up() pen.goto(0, 210) pen.setheading(180) pen.color("green") pen.pendown() pen.circle(210) # Draw hour hashes pen.up() pen.goto(0, 0) pen.setheading(90) for _ in range(12): pen.fd(190) pen.pendown() pen.fd(20) pen.penup() pen.goto(0, 0) pen.rt(30) # Draw the hands # Each tuple in list hands describes the color, the length # and the divisor for the angle hands = [("white", 80, 12), ("blue", 150, 60), ("red", 110, 60)] time_set = (hr, mn, sec) for hand in hands: time_part = time_set[hands.index(hand)] angle = (time_part/hand[2])*360 pen.penup() pen.goto(0, 0) pen.color(hand[0]) pen.setheading(90) pen.rt(angle) pen.pendown() pen.fd(hand[1]) while True: hr = int(time.strftime("%I")) mn = int(time.strftime("%M")) sec = int(time.strftime("%S")) draw_clock(hr, mn, sec, pen) wndw.update() time.sleep(1) pen.clear() wndw.mainloop()
Output:
Also read:
How do you call the function?? I do not understand what pen does? can you explain further?
Hey Joe,
The code seems pretty clear. He’s using the Turtle graphics module. From context I can tell this is a Turtle vector graphics module based on the Turtle programming model often associated with Logo.
The code uses Turtle to establish a drawing context and instantiates a Turtle object in a variable called “pen”. The name pen is a giveaway that this is the object that will draw to the screen. You could look up the Python Turtle module or even just look at Turtle graphics explanation as the wikipedia page on Turtle Graphics (I tried posting the link to module and/or wikip but it said I was posting spam!).
The thing that is probably confusing you is the way the program is written mixing global namespace and locally defined functions. Since all of this is based on free software, you could just run it and play around with the code until it is clear. It is not complicated at all. There is no function to call. You asked “how do you call the function?” There is no function. The function technically is main. Typically I encapsulate the code for main in a function and then declare main and call that function. This makes the intentions crystal clear. But it’s not strictly necessary. Imagine the entire code shown is one function called “do_clock()”. WHen you call do_clock(), it never returns. It loops forever updating the clock each iteration of the endless loop.
Hi! I have been going over this recently but am lost on this part:
hands = [(“white”, 80, 12), (“blue”, 150, 60), (“red”, 110, 60)]
time_set = (hr, mn, sec)
for hand in hands:
time_part = time_set[hands.index(hand)]
angle = (time_part/hand[2])*360
Does the line of code “time_part = time_set[hands.index(hand)]” simply link hands and time_set?
I am totally lost on the angle also. Can anyone please explain why the time_part is even in there? Why could it not just be angle=(1/hand[2])*360?
If anyone could explain what the time_part actually means, I would appreciate it.