Python Turtle Clock – How to Draw

In this Python tutorial, we will learn How to draw a turtle clock in Python Turtle and we will also cover different examples related to turtle clock. And, we will cover these topics.

  • Python turtle clock
  • Python turtle clock face
  • Python turtle digital clock
  • Python turtle circle clockwise
  • Python turtle analog clock
  • Python turtle clock background-color

Python turtle clock

In this section, we will learn about how to draw a turtle clock in Python turtle.

As we know the clock is used for measuring time. The clock indicates hours, minutes, and seconds by hands-on moving in a clockwise direction.

Turtle is used for drawing the clock it is the same as a normal clock which is also used for measuring time.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

  • turtle.Screen() is used to show the screen on which we draw the shapes.
  • clock.color(‘cyan’) is used to give the color to the clock.
  • clock.width(5) is used to set the clock width.
  • clock.penup() is used to pick up the pen.
  • clock.right(90) is used to move the turtle in the right direction.
  • clock.forward(100) is used to move the turtle in the forward direction.
  • val += 1 is used in increment value by 1.
  • clock.fillcolor(‘Green’) is used to fill the color in the clock.
  • clock.circle(7) is used to draw the circle with radius 7.
  • clock.begin_fill() is used to start filling color in the clock.
  • clock.end_fill() is used to end the filling color in the clock.
from turtle import *
import turtle

scr = turtle.Screen()
 
scr.setup(500, 500)

clock = turtle.Turtle()
 
clock.color('cyan')
 
clock.width(5)
 
 
def drawhourhand():
    clock.penup()
    clock.home()
    clock.right(90)
    clock.pendown()
    clock.forward(100)
 

val = 0
 
for i in range(12):
   
    val += 1
 
    clock.penup()
 
     clock.setheading(-30 * (i + 3) + 75)
 
    clock.forward(22)

    clock.pendown()
 
    clock.forward(15)
 
    clock.penup()
 
    clock.forward(20)
 
    clock.write(str(val), align="center",
              font=("Arial",
                    12, "normal"))
clock.setpos(2, -112)
clock.pendown()
clock.width(2)

clock.fillcolor('Green')
 
clock.begin_fill()
 
clock.circle(7)
 
clock.end_fill()
 
clock.penup()
drawhourhand()
clock.setpos(-18, -62)
clock.pendown()
clock.penup()
 
clock.setpos(-28, -150)
clock.pendown()
clock.write('Python Guides', font=("Arial",14,
                              "normal"))
clock.hideturtle()
turtle.done()

Output:

After running the above code, we get the following output in which we can see the clock is drawn on the screen.

Python turtle clock
Python turtle clock Output

Also, check: Python Turtle Graphics

Python turtle clock face

In this section, we will learn about how to draw a clock face in a python turtle.

The clock face is a part of a clock that displays the time it consists of hands which indicates the hours, minutes, and seconds. These needles which show time complete the clock face.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle, and also import the DateTime module. The turtle() method is used to make objects.

  • tur.pensize(5) is used to give the size to the turtle.
  • tur.fillcolor(color) is used to fill color in the clock.
  • tur.pencolor(“brown”) is used to give a brown color to the turtle.
  • tur.circle(-r,360//mins) is used to draw a circle with given radius.
  • tur.right(90) is used to move the turtle in the right direction.
  • tur.forward(leng) is used to move the turtle in the forward direction
  • tur.title(“Clock Face”) is used to give the title to our window.
from turtle import *
import turtle as tur
import datetime
import time

def drawclock(r,hrs,mins,leng,color):
    tur.pensize(5)
    tur.penup()
    tur.goto(0,r)
    tur.pendown()
    tur.fillcolor(color)
    tur.pencolor("brown")
    tur.begin_fill()
    for degree in range(0,360,360//mins):
        tur.circle(-r,360//mins)
        tur.right(90)
        tur.forward(leng/3)
        tur.back(leng/3)
        tur.left(90)
    tur.end_fill()
    tur.pencolor("brown")
    for degree in range(0,360,360//hrs):
        tur.circle(-r,360//hrs)
        tur.right(90)
        tur.forward(leng)
        tur.back(leng)
        tur.left(90)
    tur.circle(-r)

def draw_hand(ang, r, wid, color, outline=False):
    if outline:
        tur.pencolor("brown")
    else:
        tur.pencolor(color)
    tur.pensize(4)
    tur.penup()
    tur.home()
    tur.fillcolor(color)
    tur.begin_fill()
    tur.left(90)
    tur.right(ang)
    tur.forward(r)
    tur.pendown()
    tur.left(150)
    tur.forward(wid)
    tur.home()
    tur.left(90)
    tur.right(ang)
    tur.penup()
    tur.forward(r)
    tur.pendown()
    tur.right(150)
    tur.forward(wid)
    tur.home()
    tur.end_fill()

r=200
tur.speed(0)
tur.hideturtle()
tur.title("Clock Face")
drawclock(r,12,60,r*.1,"cyan")
current_time=datetime.datetime.now().time()
tur.title(current_time.strftime("%H:%M Clock face"))
draw_hand(current_time.minute * 6, r * .9, r // 10, "gray")
draw_hand(((current_time.hour + current_time.minute / 60) % 12) * 30, r * .6, r // 10, "brown")

while True:
    new_time=datetime.datetime.now().time()
    if current_time.minute is not new_time.minute:
        tur.title(new_time.strftime("%H:%M Clock Face"))
        draw_hand(current_time.minute * 6, r * .9, r // 10, "cyan")
        draw_hand(((current_time.hour + current_time.minute / 60) % 12) * 30, r * .6, r // 10, "cyan")
        draw_hand(new_time.minute * 6, r * .9, r // 10, "gray")
        draw_hand(((new_time.hour + new_time.minute / 60) % 12) * 30, r * .6, r // 10, "black")
    current_time=new_time
    time.sleep(50)

Output:

After running the above code, we get the following output in which we can see the clock face with their hands which indicates the hour, minute, and second.

Python turtle clock face
Python turtle clock face Output

Read: Python turtle onclick with examples

Python turtle digital clock

In this section, we will learn about how to draw a digital clock in Python turtle.

The digital clock displays the time like an analog clock but it displays the time in numerical digits rather than a hands-on dial. Turtle digital clock is drawn by a turtle and displays the time digitally.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle, and also import datetime module. The turtle() method is used to make objects.

  • turtle.Screen() is used to create a screen in which we can draw the shapes.
  • screen.bgcolor(“cyan”) is used to give the background color to the screen.
  • second = datetime.datetime.now().second is used to obtain current second from the screen.
  • minute = datetime.datetime.now().minute is used to obtain current minute from the screen.
  • hour = datetime.datetime.now().hour is used to obtain current hour from the screen.
  • tur1.goto(-20, 0) is used to set the position of turtle.
  • tur1.forward(200) is used for moving the turtle in the forwarding direction to create a rectangular box.
  • tur1.left(90) is used for moving the turtle in the left direction to create a rectangular box.
from turtle import *
import time
import datetime 
import turtle
  
tur  = turtle.Turtle()
 
tur1 = turtle.Turtle()

screen = turtle.Screen()

screen.bgcolor("cyan")

second = datetime.datetime.now().second
minute = datetime.datetime.now().minute
hour = datetime.datetime.now().hour
tur1.pensize(4)
tur1.color('red')
tur1.penup()
  
tur1.goto(-20, 0)
tur1.pendown()

for i in range(2):
    tur1.forward(200)
    tur1.left(90)
    tur1.forward(70)
    tur1.left(90)

tur1.hideturtle()
 
while True:
    tur.hideturtle()
    tur.clear()
   
    tur.write(str(hour).zfill(2)
            +":"+str(minute).zfill(2)+":"
            +str(second).zfill(2),
            font =("Arial", 35, "bold"))
    time.sleep(1)
    second+= 1
     
    if second == 60:
        second = 0
        minute+= 1
     
    if minute == 60:
        minute = 0
        hour+= 1
     
    if hour == 13:
        hour = 1

Output:

After running the above code, we get the following output in which we can see the digital clock on the screen which shows the current time.

Python turtle digital clock
Python turtle digital clock Output

Read: Python Turtle Tracer

Python turtle circle clockwise

In this section, we will learn about how to draw a turtle circle clockwise in Python turtle.

As we know the clock is in the shape of a circle. Here we draw a circle that moves in a clockwise direction like the clock our circle is exactly moved in clockwise.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

tur.circle(-50) is used to draw a circle in a clockwise direction.

from turtle import *
import turtle
    
tur = turtle.Turtle()
  
tur.circle(-50)
turtle.done()

Output:

After running the above code, we get the following output in which we can see the circle is drawn on the screen in a clockwise direction.

Python turtle circle clockwise
Python turtle circle clockwise Output

Read: Python Turtle Triangle + Examples

Python turtle analog clock

In this section, we will learn about how to draw a turtle analog clock in a python turtle.

An Analog clock is a clock that represents the time that is shown with the help of hands and the hours, minutes, and seconds are indicated by these hands.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle, and import time. The turtle() method is used to make objects.

  • ws.bgcolor(“black”) is used for giving the background color to the screen.
  • ws.title(“Analog Clock”) is used for giving the title to the window.
  • tur.speed(0) is used for giving the speed to the turtle and 0 is the fastest speed.
  • tur.pensize(3) is used for giving the size to turtle.
  • tur.circle(210) is used for drawing the circle with the help of a turtle.
  • clockhands = [(“red”, 80, 12), (“white”, 150, 60), (“blue”, 110, 60)] is used for giving the color to the hands of clock.
  • tur.fd(190) is used for moving the turtle in the forward direction.
from turtle import *
import turtle
import time

ws = turtle.Screen()
ws.bgcolor("black")
ws.setup(width=600, height=600)
ws.title("Analog Clock")
ws.tracer(0)
tur = turtle.Turtle()
tur.hideturtle()
tur.speed(0)
tur.pensize(3)
def draw_clock(hour, min, second, tur):
    tur.up()
    tur.goto(0, 210)
    tur.setheading(180)
    tur.color("red")
    tur.pendown()
    tur.circle(210)
    tur.up()
    tur.goto(0, 0)
    tur.setheading(90)
    for _ in range(12):
        tur.fd(190)
        tur.pendown()
        tur.fd(20)
        tur.penup()
        tur.goto(0, 0)
        tur.rt(30)
   
    clockhands = [("red", 80, 12), ("white", 150, 60), ("blue", 110, 60)]
    timeset = (hour, min, second)
    for hand in clockhands:
        timepart = timeset[clockhands.index(hand)]
        angle = (timepart/hand[2])*360
        tur.penup()
        tur.goto(0, 0)
        tur.color(hand[0])
        tur.setheading(90)
        tur.rt(angle)
        tur.pendown()
        tur.fd(hand[1])
while True:
    hour = int(time.strftime("%I"))
    min = int(time.strftime("%M"))
    second = int(time.strftime("%S"))
    draw_clock(hour, min, second, tur)
    ws.update()
    time.sleep(1)
    tur.clear()
window.mainloop()

Output:

After running the above code, we get the following output in which we can see the analog clock is shown on the screen.

Python turtle analog clock
Python turtle analog clock Output

Read: Python Turtle Font

Python turtle clock background-color

In this section, we will learn about how to change the background color of the turtle clock in python turtle.

The clock is used to measure the time and it helps the people to do their work on time.

Here we can draw the clock with the help of turtle and also give the background color which makes the clock more beautiful and attractive.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle, and import time. The turtle() method is used to make objects.

  • ws.bgcolor(“light blue”) is used for giving the background color to the screen.
  • ws.title(“Analog Clock”) is used for giving the title to the window.
  • tur.speed(0) is used for giving the speed to the turtle and 0 is the fastest speed.
  • tur.pensize(3) is used for giving the size to turtle.
  • tur.circle(210) is used for drawing the circle with the help of a turtle.
  • clockhands = [(“pink”, 80, 12), (“yellow”, 150, 60), (“brown”, 110, 60)] is used for giving the color to the hands of clock.
  • tur.fd(190) is used for moving the turtle in the forward direction.
from turtle import *
import turtle
import time

ws = turtle.Screen()
ws.bgcolor("light blue")
ws.setup(width=600, height=600)
ws.title("Python Guides")
ws.tracer(0)

tur = turtle.Turtle()
tur.hideturtle()
tur.speed(0)
tur.pensize(3)
def draw_clock(hour, min, second, tur):
    
    tur.up()
    tur.goto(0, 210)
    tur.setheading(180)
    tur.color("purple")
    tur.pendown()
    tur.circle(210)
   
    tur.up()
    tur.goto(0, 0)
    tur.setheading(90)
    for _ in range(12):
        tur.fd(190)
        tur.pendown()
        tur.fd(20)
        tur.penup()
        tur.goto(0, 0)
        tur.rt(30)
   
    clockhands = [("pink", 80, 12), ("yellow", 150, 60), ("brown", 110, 60)]
    timeset = (hour, min, second)
    for hand in clockhands:
        timepart = timeset[clockhands.index(hand)]
        angle = (timepart/hand[2])*360
        tur.penup()
        tur.goto(0, 0)
        tur.color(hand[0])
        tur.setheading(90)
        tur.rt(angle)
        tur.pendown()
        tur.fd(hand[1])
while True:
    hour = int(time.strftime("%I"))
    min = int(time.strftime("%M"))
    second = int(time.strftime("%S"))
    draw_clock(hour, min, second, tur)
    ws.update()
    time.sleep(1)
    tur.clear()

Output:

After running the above code, we get the following output in which we can see the beautiful background color of the clock.

Python turtle clock background color
Python turtle clock background-color Output

You may also like to read the following Python Turtle tutorials.

Here, we learned Python Turtle Clock and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python turtle clock
  • Python turtle clock face
  • Python turtle digital clock
  • Python turtle circle clockwise
  • Python turtle analog clock
  • Python turtle clock background-color