Draw a Digital Clock in Python Using Turtle

Draw a Digital Clock in Python Using Turtle
(Last Updated On: 29/05/2023)

Python Program to Draw Digital Clock Using Turtle

# Python program to draw digital clock using Turtle
import time
import datetime as dte
import turtle
# Create a turtle to display time
t1 = turtle.Turtle()
# Create a turtle to create rectangle box
t2 = turtle.Turtle()
# Create screen
screen = turtle.Screen()
# Set background color of the screen
screen.bgcolor("orange")
# Obtain current hour, minute and second from the system
sec = dte.datetime.now().second
min = dte.datetime.now().minute
hr = dte.datetime.now().hour
t2.pensize(3)
t2.color('black')
t2.penup()
# Set the position of turtle
t2.goto(-5, 0)
t2.pendown()
# Create rectangular box
for i in range(2):
   t2.forward(200)
   t2.left(90)
   t2.forward(70)
   t2.left(90)
# Hide the turtle
t2.hideturtle()
while True:
   t1.hideturtle()
   t1.clear()
   # Display the time
   t1.write(str(hr).zfill(1)
            + ":"+str(min).zfill(1)+":"
            + str(sec).zfill(1),
            font=("Arial Narrow", 35, "bold"))
   time.sleep(1)
   sec += 1
   if sec == 60:
       sec = 0
       min += 1
   if min == 60:
       min = 0
       hr += 1
   if hr == 13:
       hr = 1

Output:

Draw a Digital Clock in Python Using Turtle

Leave a Reply

Your email address will not be published. Required fields are marked *