Fundamentals 9 min read

Create a Real‑Time Analog Clock with Python Turtle

This article demonstrates how to use Python's built-in Turtle module to create an analog clock that displays the current time, including step-by-step explanations of setting up the Turtle objects, drawing the clock face, hands, and updating them in real time with code examples.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Create a Real‑Time Analog Clock with Python Turtle

Program Introduction

Python provides a simple way to create graphical programs using the built‑in Turtle module. Turtle can draw lines, circles, and text, making it ideal for beginners to learn basic graphics programming.

Core Concepts

A Turtle object is created when the module is imported. Its initial position is at the center of the window (0,0) facing right. When the pen is down, moving the turtle draws a line from the previous position to the new one.

In this tutorial we will build an analog clock that shows the current time.

Key Functions

Skip(step) – lifts the pen, moves forward by step, then puts the pen down.

def Skip(step):
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()

mkHand(name, length) – registers a custom shape for a clock hand.

def mkHand(name, length):
    turtle.reset()
    Skip(-length * 0.1)
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    turtle.end_poly()
    handForm = turtle.get_poly()
    turtle.register_shape(name, handForm)

Init() – creates three hand turtles (second, minute, hour) and a printer turtle for text.

def Init():
    global secHand, minHand, hurHand, printer
    turtle.mode("logo")
    mkHand("secHand", 135)
    mkHand("minHand", 125)
    mkHand("hurHand", 90)
    secHand = turtle.Turtle()
    secHand.shape("secHand")
    minHand = turtle.Turtle()
    minHand.shape("minHand")
    hurHand = turtle.Turtle()
    hurHand.shape("hurHand")
    printer = turtle.Turtle()
    printer.hideturtle()
    printer.penup()

Week(t) and Date(t) format the current weekday and date.

def Week(t):
    week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    return week[t.weekday()]

def Date(t):
    y = t.year
    m = t.month
    d = t.day
    return "%s %02d-%02d" % (y, m, d)

Tick() updates the hands and displays the time.

def Tick():
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60.0
    hour = t.hour + minute / 60.0
    secHand.setheading(6 * second)
    minHand.setheading(6 * minute)
    hurHand.setheading(30 * hour)
    turtle.tracer(False)
    printer.forward(65)
    printer.write(Week(t), align="center", font=("Courier", 14, "bold"))
    printer.back(130)
    printer.write(Date(t), align="center", font=("Courier", 14, "bold"))
    printer.home()
    turtle.tracer(True)
    turtle.ontimer(Tick, 100)

SetupClock(radius) draws the clock face with hour marks.

def SetupClock(radius):
    turtle.reset()
    turtle.pensize(7)
    for i in range(60):
        Skip(radius)
        if i % 5 == 0:
            turtle.forward(20)
            Skip(-radius - 20)
            Skip(radius + 20)
            if i == 0:
                turtle.write(12, align="center", font=("Courier", 14, "bold"))
            elif i == 30:
                Skip(25)
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
                Skip(-25)
            elif i == 25 or i == 35:
                Skip(20)
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
                Skip(-20)
            else:
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
            Skip(-radius - 20)
        else:
            turtle.dot(5)
            Skip(-radius)
        turtle.right(6)

Finally, combine the functions to start the clock:

Init()
SetupClock(200)
Tick()
Clock example
Clock example

Feel free to modify the Turtle drawing to create more interesting graphics.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

datetimeTutorialturtleanalog clock
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.