Javatpoint Logo
Javatpoint Logo

Creating a digital clock using Turtle in Python

Python programming language turtle. Python has a unique feature called Turtle. We can quickly sketch on a piece of paper with Turtle. We import the turtle package first. Next, make a window. After that, make a turtle object so that you may use it to draw on a piece of paper. Using turtle and Python, we'll build a digital clock in this blog.

Installing the turtle library :

Type the following command in the terminal to install this module.

Notice that we will also use Python's "time" and "DateTime" modules to build a clock.

Python digital clock with turtle :

This section will teach you how to use Python's Turtle to create a digital clock. Make sure you are familiar with the fundamentals of Turtle before we begin learning how to use it to create a digital clock in Python. To find out more information on Turtle programming in Python keep in touch with us.

Creating a digital clock using Turtle in Python

The clock we use will resemble the one seen above. Let's now examine how to create a digital clock in Python using Turtle.

Approach to be used :

To create a digital clock in Python using Turtle, follow these instructions :

  • import every module ( time, datetime, turtle).
  • Then make two turtles, one to draw a box that is rectangular and the other to display the time.
  • Create a window now, and use the window's bgcolor() function to set the background colour to pink.
  • Position the turtle, draw a rectangle, and afterwards conceal it.
  • Afterward, try to find the current time on our system (hour, minute, second). The datetime module will then be used for this.
  • Use the other turtle object we have to display the time at this point.

Let's now examine the Python code for drawing a digital clock with Turtle.

Implementation :

First of all, we will import all the required libraries like turtle, time and datetime to our programme.

Next, we created two turtles namely tt1 and tt2 for displaying time and creating a rectangle, respectively.

Then, we created the screen using screen() method and gave it a background colour of our choice, using bgcolor() method.

Further, we used the now() method to obtain the current hour, minutes and seconds of the system in use.

We also set the size of the pen using pensize() method and set the colour of the pen ink as white.

Next, we created a rectangular box using a for loop executing two times. In that for loop, forward() method was used to advance the turtle by a number of points and the left() method was used to turn the turtle to the left.

Ultimately, we displayed the current time using the str() method with each quantity having maximum digits limit as 2, as mentioned by zfill() method. Finally, we set the font of the time to be displayed in, using font() method and displayed the time with a sleep time of 1 second, i.e. blink time.

Complete Code :

Output:

Creating a digital clock using Turtle in Python





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA