Table of contents
1.
Introduction
2.
Digital Clock using Turtle in Python
3.
Steps
3.1.
Code
4.
Output
5.
Frequently Asked Questions
5.1.
What are the steps used to plot using Turtle?
5.2.
How to create a new turtle object?
5.3.
What are the attributes of the turtle library?
5.4.
Is it possible to combine turtle and Tkinter?
5.5.
In Python, how can you move a turtle without drawing?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Digital Clock using Turtle in Python

Author Saurabh Anand
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Turtle is an inbuilt module in Python similar to the virtual canvas. We can use it to draw pictures and attractive shapes. It provides us with a screen (cardboard) and a turtle (pen). We need to move the pen to draw something on the screen. To move pen, there are some functions, i.e backward(), forward(), etc.

 In this blog, we will learn how to draw a digital clock using Turtle in Python, along with the steps and code.

Check out this article Fibonacci Series in Python here.

Digital Clock using Turtle in Python

In this section, we will learn how to draw a digital clock using Turtle in Python. Before we start learning how to draw a digital clock using Turtle in Python, be sure that you know the Turtle basics. To learn more about Turtle programming in Python, refer to the blog Turtle Programming in Python.

Digital Clock


Our clock will look like the above image. Now let's look at the steps to draw a digital clock using Turtle in Python.

Steps

These steps need to be followed to draw a digital clock using Turtle in Python:

  • Import all the modules( time, datetime, turtle).
  • Then, Create two turtles, one for making a rectangular box and another for showing the current time.
  • Now, Create a window and set the background color using bgcolor() function of the window to orange.
  • Set the turtle's position, create a rectangle, and then hide the turtle.
  • After that, try to obtain our system's current time (hour, minute, second). Then, we will use our datetime module for this.
  • Now, Use our other turtle object to show the current time.

Now let's look at the code to draw a digital clock using Turtle in Python.

Code

# 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(-20, 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(2)
            + ":"+str(min).zfill(2)+":"
            + str(sec).zfill(2),
            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
You can also try this code with Online Python Compiler
Run Code

 

Check out this article - Quicksort Python

Output

You can compile it with online python compiler.

You can also read about Palindrome Number in Python here.

Frequently Asked Questions

What are the steps used to plot using Turtle?

The steps to plot a turtle program are as follows:  

Step 1: Import the turtle module.

Step 2: Create a turtle to control.

Step 3: Draw shapes using the turtle methods.

Step 4: Run turtle.done() function.

How to create a new turtle object?

The Turtle() method creates and returns a new turtle object in Python.

What are the attributes of the turtle library?

A location, an orientation (or direction), and a pen are the three attributes of the turtle. Color, width, and on/off state are all pen attributes (also called down and up). "Move forward 10 spaces" and "turn left 90 degrees" are commands that the turtle responds to based on its current location.

Is it possible to combine turtle and Tkinter?

Yes. Python turtle has two modes of operation: standalone and embedded into a bigger Tkinter program. When utilizing turtle embedded, you work with RawTurtle, TurtleScreen, and optionally ScrolledCanvas instead of Turtle and Screen. As desired, you create your Tkinter interface containing your turtle graphics on a Canvas.

In Python, how can you move a turtle without drawing?

Toggle drawing on and off with up and down, or move without drawing with the "setx", "sety", or "goto" functions.

Conclusion

In this article, we have extensively discussed how to draw a digital clock using Turtle in Python, along with the steps and code.

We hope that this blog has helped you enhance your knowledge of how to draw a digital clock using Turtle in Python. If you would like to learn more, check out our article Panda Using TurtleStar using Turtle In PythonChess Board using Turtle. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass