Analog timer using turtle module in Python
In this tutorial, let’s build an analog timer using turtle in Python. Let us have circular representation of hours, minutes and seconds as distance traversed by the respective turtles.
Turtle movement calculations:
- The seconds hand of an analog clock traverses 6 degrees every second
- The minute hand traverses 6 degrees every minute; 1 min = 60 seconds -> therefore minute hand traverses 6/60 degrees every second.
- The hour hand traverses 30 degrees every hour; 1 hour = 3600 seconds -> therefore hour hand traverses 30/3600 degrees every second
Using these traversal calculations above we create 3 turtles and move them every second after a fixed delay. This delay plus the time taken to traverse all the three triangle’s each time must be equal to 1000 milliseconds to ensure that it takes 1 second in all for each tick.
We take the user input in the form of “Hours Minutes Seconds” separated by spaces and convert this to seconds. This calculation is to control number of ticks of the three turtles.
Python Code: Analog timer
import turtle # import turtle for graphical representation of Analog timer clock import time # importing time to measure time and track duration import math duration = list(map(int, input("Enter the duration of timer in hour minutes and seconds format separated by spaces:\n").strip().split())) number_of_ticks = duration[0] * 3600 + duration[1] * 60 + duration[2] # print(number_of_ticks) # number of ticks is the entire time converted into seconds to determine the number of times the turtle must tick second_extent = 6 # second hand traverses 6 degrees every second in a clock minute_extent = 6 / 60 # minute hand traverses 6 degrees every minute so it traverses 6/60 degrees every second hour_extent = 30 / 3600 # hour hand traverses 30 degrees every hour so it traverses 30/3600 degrees every second second_turtle = turtle.Turtle() # creating a turtle that represents seconds traversed minute_turtle = turtle.Turtle() # creating a turtle that represents minutes traversed hour_turtle = turtle.Turtle() # creating a turtle that represents hours traversed window = turtle.Screen() window.bgcolor("#F5F0E1") # Half white background of turtle screen window.setup(400, 400) # changing turtle window size for aesthetics of the window def circle_draw(): # function to move turtles every # note: radius is negative to move the turtles in clockwise direction second_turtle.circle(-100,extent=second_extent) minute_turtle.circle(-120, extent=minute_extent) hour_turtle.circle(-140, extent=hour_extent) def turtle_settings(): # turtle speeds second_turtle.speed(0) minute_turtle.speed(0) hour_turtle.speed(0) # turtle colors second_turtle.color("#1E3D59") # blue minute_turtle.color("#FF6E40") # orange hour_turtle.color("#FFC13B") # yellow # turtle pen sizes/ stroke sizes hour_turtle.pensize(4) minute_turtle.pensize(3) second_turtle.pensize(2) # second hand turtles starting position second_turtle.penup() second_turtle.goto(0, 100) second_turtle.pendown() # minute hand turtles starting location minute_turtle.penup() minute_turtle.goto(0, 120) minute_turtle.pendown() # hour hands starting location hour_turtle.penup() hour_turtle.goto(0, 140) hour_turtle.pendown() def turtle_reset(): second_turtle.reset() minute_turtle.reset() hour_turtle.reset() def time_conversion(time_elapsed): # converting seconds to hours:minutes:seconds format hour = math.floor(time_elapsed / 3600) minute = math.floor((time_elapsed % 3600) / 60) second = math.floor((time_elapsed % 3600) % 60) print(f'Time elapsed: {hour}:{minute}:{second}\n') turtle_settings() # setting up the turtles with customisation and positions calibration_start = time.time() circle_draw() calibration_end = time.time() draw_time = (calibration_end - calibration_start) # calculating time taken to move the three turtles #print(draw_time) uncomment and remove this line to see the time required to move the three turtles each time in milliseconds turtle_reset() # calling turtle reset to clear the drawings and turtle settings turtle_settings() # turtle settings to customise turtles and assign their positions ''' delay between any two function calls of circle draw to ensure that that time gap between any two calls and the time to taken to move the three turtles together makes 1000 milliseconds ''' compensated_delay = int(1000 - round((1000 * draw_time), 3)) # print(compensated_delay) uncomment to see the delay start = time.time() for i in range(1, number_of_ticks + 1): # to move the turtles every second until the duration is complete turtle.ontimer(circle_draw(), compensated_delay) # circle draw is called after a duration of compensated delay # so that time take by circle_draw's execution + compensated delay makes 1000 milli seconds end_time = time.time() time_conversion(end_time - start) # converting seconds to hours:minutes:seconds format window.bgcolor("#C6D7EB") # changing background color of turtle window to indicate end of timer window.exitonclick() # to prevent screen from shutting down immediately after timer stops
Output:
Enter the duration of timer in hour minutes and seconds format separated by spaces: 00 30 12 Time elapsed: 00:30:52
Turtle window when timer is running:
Turtle window when timer stops:
Note: There is time drift of 40 seconds every 30 mins.
Also read: Analog stopwatch using Python turtle module
Leave a Reply