How to Create a Digital Clock Using Turtle-Python

Time flies. To keep track of it, one of the greatest invention was the clock. We have come a long way transforming from the analogue clock to the digital clock, with a more versatile functionality. Is it not great to build your own digital clock from scratch? Don’t worry, your dream will come true in minutes. In this blog, I am going to teach you how to build one.

So, let’s get started.

Create Digital Clock Project

Requirements :  

Basic knowledge of python(go through Learn Python Tutorial) . We can use any ide ( for example:  https://code.visualstudio.com/download , https://jupyter.org/).

For better understanding go through https://studygyaan.com/python/getting-started-with-turtle-programming-in-python .

Modules :

Turtle module: It is a python inbuilt module similar to a canvas that lets us command a turtle to draw attractive pictures and shapes on it.

Time: There is a popular time module available in Python which provides many ways of representing time in code, such as objects, numbers, and strings.

Datetime: This inbuilt module in Python supplies classes to work with time and date.

Installation :

All of them are inbuilt modules ,so we do not have to install them.

Approach :

1. Firstly, we have to import all the modules( time, datetime, turtle).

 import time
 import datetime as dt
 import turtle 

2. Secondly, we have to create two turtles, one for making a rectangular box and another for showing the current time.

 t1 = turtle.Turtle()
 t2 = turtle.Turtle() 

3. Thirdly, we will create a window and set the background color of the window.

 MyScreen = turtle.Screen()
 MyScreen.bgcolor( ) 

4. Fourthly, we have to set the position of the turtle and we will create a rectangle and at last we will hide the turtle.

 t2.pensize(3)
 t2.color( )
 t2.penup()
 t2.goto(-20, 0)
 t2.pendown()
 for i in range(2):
        t2.forward(200)
        t2.left(90)
        t2.forward(70)
        t2.left(90)
 t2.hideturtle() 

5. After that, we will try to obtain current time (hour, minute, second) from the system. We will use our datetime module for this.

 sec = dt.datetime.now().second
 min = dt.datetime.now().minute
 hr = dt.datetime.now().hour 

6. Then, we will use our other turtle object for showing the current time.

t1.write(str(hr).zfill(2) +":"+str(min).zfill(2)+":" +str(sec).zfill(2), font =( ))

Source Code:

 import time
 import datetime as dt
 import turtle
 t1 = turtle.Turtle()
 t2 = turtle.Turtle()
 MyScreen = turtle.Screen()
 MyScreen.bgcolor("green")
 sec = dt.datetime.now().second
 min = dt.datetime.now().minute
 hr = dt.datetime.now().hour
 t2.pensize(3)
 t2.color('black')
 t2.penup()
 t2.goto(-20, 0)
 t2.pendown()
 for i in range(2):
       t2.forward(200)
       t2.left(90)
       t2.forward(70)
       t2.left(90)
 t2.hideturtle()
 while True:
       t1.hideturtle()
       t1.clear()
       t1.write(str(hr).zfill(2) +":"+str(min).zfill(2)+":" +str(sec).zfill(2), font =( ))
       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 :

Digital Clock Using Turtle-Python