Create a Python Turtle Clock

I was working on a Python project where I needed to create a visual clock using the Turtle module. The challenge was interesting because it combined basic programming concepts with visual elements that make coding more engaging, especially for beginners.

In this article, I’ll walk you through how to create your analog clock using Python’s Turtle graphics. I’ve broken it down into simple steps that anyone can follow, even if you’re just starting with Python.

So let’s get in!

Understand Python Turtle Graphics

Python’s Turtle module is a built-in library that allows you to create graphics by directing a virtual “turtle” around the screen. It’s perfect for beginners because it provides immediate visual feedback for your code.

Before we start building our clock, let’s make sure we understand the basics of Turtle graphics.

Method 1: Build a Basic Analog Clock

Let’s create a simple analog clock with hour, minute, and second hands.

import turtle
import time

# Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Python Turtle Clock")
wn.setup(width=600, height=600)

# Create the clock face
clock_face = turtle.Turtle()
clock_face.speed(0)
clock_face.pensize(3)
clock_face.color("white")

# Draw the clock circle
clock_face.penup()
clock_face.goto(0, -210)
clock_face.pendown()
clock_face.begin_fill()
clock_face.circle(210)
clock_face.color("gray10")
clock_face.end_fill()

# Draw the hour markers
clock_face.color("white")
for i in range(12):
    clock_face.penup()
    clock_face.goto(0, 0)
    clock_face.setheading(90 - (i * 30))
    clock_face.forward(180)
    clock_face.pendown()
    clock_face.forward(30)

# Create hour hand
hour_hand = turtle.Turtle()
hour_hand.speed(0)
hour_hand.pensize(6)
hour_hand.color("white")
hour_hand.shape("arrow")
hour_hand.shapesize(0.5, 8)

# Create minute hand
minute_hand = turtle.Turtle()
minute_hand.speed(0)
minute_hand.pensize(4)
minute_hand.color("white")
minute_hand.shape("arrow")
minute_hand.shapesize(0.4, 12)

# Create second hand
second_hand = turtle.Turtle()
second_hand.speed(0)
second_hand.pensize(2)
second_hand.color("red")
second_hand.shape("arrow")
second_hand.shapesize(0.3, 14)

# Write the numbers
clock_face.penup()
clock_face.goto(0, 0)
for i in range(1, 13):
    clock_face.penup()
    # Position numbers slightly inside the markers
    angle = 90 - (i * 30)
    clock_face.setheading(angle)
    clock_face.forward(160)
    clock_face.write(str(i), align="center", font=("Arial", 20, "bold"))

clock_face.hideturtle()

while True:
    # Get current time
    current_time = time.localtime()
    hour = current_time.tm_hour % 12
    minute = current_time.tm_min
    second = current_time.tm_sec

    # Set the hands to the correct position
    # Hour hand: 30 degrees per hour + small adjustment for minutes
    hour_angle = 90 - ((hour * 30) + (minute / 2))
    hour_hand.setheading(hour_angle)

    # Minute hand: 6 degrees per minute
    minute_angle = 90 - (minute * 6)
    minute_hand.setheading(minute_angle)

    # Second hand: 6 degrees per second
    second_angle = 90 - (second * 6)
    second_hand.setheading(second_angle)

    wn.update()
    time.sleep(1)

I executed the above example code and added the screenshot below.

turtle clock

In this code, I’ve created:

  • A circular clock face with hour markers
  • Hour, minute, and second hands with different colors and sizes
  • Numeric indicators for each hour

The clock updates every second to reflect the current time.

Read Python Turtle 3d Shapes

Method 2: Create a Digital Clock with Turtle

If you prefer a digital display, you can create one using Turtle as well:

import turtle
import time

# Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Python Turtle Digital Clock")
wn.setup(width=600, height=300)

# Create text turtle
digital_clock = turtle.Turtle()
digital_clock.hideturtle()
digital_clock.penup()
digital_clock.color("cyan")

while True:
    # Clear previous time
    digital_clock.clear()

    # Get current time
    current_time = time.strftime("%I:%M:%S %p")

    # Display the time
    digital_clock.goto(0, 0)
    digital_clock.write(current_time, align="center", font=("Arial", 60, "bold"))

    # Update every second
    wn.update()
    time.sleep(1)

I executed the above example code and added the screenshot below.

arial tur

This digital clock displays the current time in hours, minutes, seconds, and AM/PM format.

Check out Python Turtle Polygon

Method 3: Advanced Analog Clock with Date Display

Let’s combine both concepts and create a more advanced clock that shows both the analog time and the current date:

import turtle
import time
import datetime

# Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Python Turtle Advanced Clock")
wn.setup(width=600, height=600)

# Create the clock face
clock_face = turtle.Turtle()
clock_face.speed(0)
clock_face.pensize(3)
clock_face.color("white")

# Draw the clock circle
clock_face.penup()
clock_face.goto(0, -210)
clock_face.pendown()
clock_face.begin_fill()
clock_face.circle(210)
clock_face.color("navy")
clock_face.end_fill()

# Draw the hour markers
clock_face.color("white")
for i in range(12):
    clock_face.penup()
    clock_face.goto(0, 0)
    clock_face.setheading(90 - (i * 30))
    clock_face.forward(180)
    clock_face.pendown()
    clock_face.forward(30)

# Create clock hands
hour_hand = turtle.Turtle()
hour_hand.speed(0)
hour_hand.pensize(6)
hour_hand.color("white")
hour_hand.shape("arrow")
hour_hand.shapesize(0.5, 8)

minute_hand = turtle.Turtle()
minute_hand.speed(0)
minute_hand.pensize(4)
minute_hand.color("white")
minute_hand.shape("arrow")
minute_hand.shapesize(0.4, 12)

second_hand = turtle.Turtle()
second_hand.speed(0)
second_hand.pensize(2)
second_hand.color("red")
second_hand.shape("arrow")
second_hand.shapesize(0.3, 14)

# Date display
date_display = turtle.Turtle()
date_display.speed(0)
date_display.hideturtle()
date_display.penup()
date_display.color("white")
date_display.goto(0, -50)

# Write the numbers
clock_face.penup()
clock_face.goto(0, 0)
for i in range(1, 13):
    clock_face.penup()
    angle = 90 - (i * 30)
    clock_face.setheading(angle)
    clock_face.forward(160)
    clock_face.write(str(i), align="center", font=("Arial", 20, "bold"))

clock_face.hideturtle()

while True:
    # Get current time
    now = datetime.datetime.now()
    hour = now.hour % 12
    minute = now.minute
    second = now.second

    # Update date display
    date_display.clear()
    date_str = now.strftime("%A, %B %d, %Y")
    date_display.write(date_str, align="center", font=("Arial", 14, "normal"))

    # Set the hands to the correct position
    hour_angle = 90 - ((hour * 30) + (minute / 2))
    hour_hand.setheading(hour_angle)

    minute_angle = 90 - (minute * 6)
    minute_hand.setheading(minute_angle)

    second_angle = 90 - (second * 6)
    second_hand.setheading(second_angle)

    wn.update()
    time.sleep(1)

I executed the above example code and added the screenshot below.

analog clock python code

This advanced version includes:

  • A full analog clock with hands
  • A date display showing the current day of the week, month, day, and year
  • A navy blue background for better contrast

Read Python Turtle Window

Method 4: Create a Themed Clock for Special Occasions

For holidays or special events, you might want to create a themed clock. Here’s an example of a New Year’s countdown clock:

import turtle
import time
import datetime

# Set up the screen
wn = turtle.Screen()
wn.bgcolor("midnight blue")
wn.title("New Year Countdown Clock")
wn.setup(width=600, height=600)

# Create text display
countdown = turtle.Turtle()
countdown.hideturtle()
countdown.penup()
countdown.color("gold")

# Create additional decoration
decorator = turtle.Turtle()
decorator.speed(0)
decorator.hideturtle()
decorator.penup()

# Draw stars in the background
for _ in range(50):
    x = turtle.randint(-280, 280)
    y = turtle.randint(-280, 280)
    decorator.goto(x, y)
    decorator.dot(turtle.randint(1, 3), "white")

# New Year title
decorator.color("red")
decorator.goto(0, 200)
decorator.write("New Year Countdown", align="center", font=("Arial", 30, "bold"))

# Set the target date (next New Year)
current_year = datetime.datetime.now().year
target_date = datetime.datetime(current_year + 1, 1, 1, 0, 0, 0)

while True:
    # Clear previous countdown
    countdown.clear()

    # Calculate time remaining
    now = datetime.datetime.now()
    time_remaining = target_date - now

    # Calculate days, hours, minutes, seconds
    days = time_remaining.days
    hours = time_remaining.seconds // 3600
    minutes = (time_remaining.seconds % 3600) // 60
    seconds = time_remaining.seconds % 60

    # Display countdown
    countdown.goto(0, 0)
    countdown_text = f"{days} days\n{hours:02d}:{minutes:02d}:{seconds:02d}"
    countdown.write(countdown_text, align="center", font=("Arial", 40, "bold"))

    # Add additional text
    countdown.goto(0, -100)
    countdown.color("cyan")
    countdown.write(f"Counting down to {target_date.year}!", align="center", font=("Arial", 20, "normal"))

    # Update every second
    wn.update()
    time.sleep(1)

This New Year’s countdown clock features:

  • A starry background
  • A title at the top
  • A large countdown display showing days, hours, minutes, and seconds until the New Year
  • A message below the countdown

Read Python Turtle Random

Customize Your Clock

The beauty of creating a clock with Python Turtle is that you can customize almost every aspect of it. Let’s explore some ways to make your clock truly unique: Changing Colors and Themes

You can easily change the colors to match your preference or create themed clocks:

# For a gold and black luxury theme
wn.bgcolor("black")
clock_face.color("gold")
hour_hand.color("gold")
minute_hand.color("gold")
second_hand.color("white")

# For a nature theme
wn.bgcolor("forest green")
clock_face.color("brown")
hour_hand.color("saddle brown")
minute_hand.color("saddle brown")
second_hand.color("coral")

Add Custom Backgrounds

You can create a custom clock by adding images as backgrounds:

# Add a custom background image
wn.bgpic("clock_background.gif")  # Must be in GIF format for Turtle

Remember that Turtle only supports GIF images by default, so you’ll need to convert your images to that format.

Add Sound Effects

For a more interactive experience, you can add sound effects that play at specific times:

import winsound  # For Windows

def play_hourly_chime():
    winsound.PlaySound("chime.wav", winsound.SND_FILENAME)

# Then in your main loop:
if minute == 0 and second == 0:  # At the top of each hour
    play_hourly_chime()

Create Alarm Functionality

You can turn your clock into an alarm clock with a few additional lines of code:

# Set alarm time
alarm_hour = 7
alarm_minute = 30

# In your main loop
if hour == alarm_hour and minute == alarm_minute and second == 0:
    for _ in range(3):  # Flash the screen as an alarm
        wn.bgcolor("red")
        time.sleep(0.5)
        wn.bgcolor("black")
        time.sleep(0.5)
    # Play alarm sound
    winsound.PlaySound("alarm.wav", winsound.SND_FILENAME)

Check out Python Turtle Hide

Real-World Applications

While this might seem like just a fun project, the concepts you learn from creating a Python Turtle clock can be applied to more complex applications:

  • Creating custom GUI elements for applications
  • Developing educational tools for teaching time
  • Building visualization tools for time-series data
  • Creating specialized timers for specific industries

I’ve personally used a modified version of this clock in a manufacturing environment to track production cycles with visual indicators.

Creating a Python Turtle clock is not just a programming exercise; it’s a creative way to explore Python’s capabilities while making something useful. Whether you’re a beginner learning the basics or an experienced developer looking for a fun project, this clock provides both educational value and practical utility.

The best part is that you can continue to expand and customize it to suit your needs, from adding new features to changing the design. So go ahead, create your own unique Python Turtle clock and watch your programming skills tick upward!

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.