Draw Clock Design using Turtle in Python

Draw Clock Design using Turtle in Python

Drawing a clock using the Turtle module in Python is a fun way to understand both the basics of Python programming and the working of a clock. Here's a simple example to draw a static clock design using the Turtle graphics library in Python:

import turtle # Setup screen win = turtle.Screen() win.bgcolor("white") win.title("Turtle Clock Design") # Create a drawing turtle pen = turtle.Turtle() pen.speed(0) # Fastest speed pen.width(3) # Set the width of the pen def draw_hand(length, angle): """Draw a single hand of the clock.""" pen.penup() pen.goto(0, 0) # Start from center pen.setheading(angle) pen.pendown() pen.forward(length) pen.penup() pen.home() # Draw clock face pen.penup() pen.goto(0, -210) # Slightly lower than center for better aesthetics pen.pendown() pen.circle(210) # Draw clock numbers font_size = 18 pen.penup() for i in range(12): angle = 360 / 12 * i x = 180 * turtle.cos(angle * 3.14 / 180) y = 180 * turtle.sin(angle * 3.14 / 180) pen.goto(x, y - font_size / 2) # Offset by half font size to center the number pen.write(str((i + 1) % 12 + 1), align="center", font=("Arial", font_size, "normal")) # Draw clock hands # For simplicity, the hands are drawn statically and don't move draw_hand(150, 90) # Hour hand, pointing to 12 draw_hand(190, 180) # Minute hand, pointing to 6 draw_hand(180, 270) # Second hand, pointing to 9 # Hide turtle pen.hideturtle() # Keep the window open turtle.done() 

In this code:

  1. We're drawing the clock face as a circle.
  2. Numbers from 1 to 12 are positioned around the clock.
  3. Three clock hands (hour, minute, second) are drawn. In this static example, they're just pointing at 12, 6, and 9 respectively for illustration purposes.

To create an actual ticking clock, you'd need to integrate this design with Python's time module to fetch the current time and update the clock hands accordingly.


More Tags

git-pull google-places distutils thymeleaf quaternions entitymanager unit-testing pagedlist azure-table-storage ng-modal

More Programming Guides

Other Guides

More Programming Examples