Monday, November 18, 2024
Google search engine
HomeLanguagesCreate digital clock using Python-Turtle

Create digital clock using Python-Turtle

Turtle is a special feature of Python. Using Turtle, we can easily draw on a drawing board. First, we import the turtle module. Then create a window, next we create a turtle object and using the turtle methods we can draw in the drawing board.

Prerequisites: Turtle Programming in Python

Installation: To install this module type the below command in the terminal. 
 

pip install turtle

Note: To create a clock we will use the ‘time’ and ‘DateTime’ modules of Python also, To install time use the following command:

Below is the implementation. 

Python3




import time
import datetime as dt
import turtle
 
 
# create a turtle to display time
t = turtle.Turtle()
 
# create a turtle to create rectangle box
t1 = turtle.Turtle()
 
# create screen
s = turtle.Screen()
 
# set background color of the screen
s.bgcolor("green")
 
# obtain current hour, minute and second
# from the system
sec = dt.datetime.now().second
min = dt.datetime.now().minute
hr = dt.datetime.now().hour
t1.pensize(3)
t1.color('black')
t1.penup()
 
# set the position of turtle
t1.goto(-20, 0)
t1.pendown()
 
# create rectangular box
for i in range(2):
    t1.forward(200)
    t1.left(90)
    t1.forward(70)
    t1.left(90)
 
# hide the turtle
t1.hideturtle()
 
while True:
    t.hideturtle()
    t.clear()
    # display the time
    t.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


Output: 

Code Explanation:

  1. The code starts by creating two turtles.
  2. The first turtle, t1, will be used to create a rectangular box on the screen.
  3. The second turtle, t2, will be used to display the time.
  4. The code then sets up some variables.
  5. The hour variable stores the current hour as a number (e.g., 13).
  6. The minute variable stores the current minute as a number (e.g., 59).
  7. And the second variable stores the current second as a number (e.g., 2).
  8. Next, the code obtains information about the system time from datetime object dt.datetime.now().second .
  9. This object contains information about the date and time that is currently being displayed on your computer or device.
  10. The next section of code sets up some variables for drawing onscreen using turtle graphics objects.
  11. These variables store information about how wide and high each line should be drawn in pixels (i.e., they are called pensize() variables), and they also specify which color to use for each line (i.e., they are called color() variables).
  12. Finally, these variables tell the turtle to start drawing at position (-20, 0) in the upper-left corner of the screen and to stop drawing. The code first creates two turtles, one to display time and another to create a rectangular box.
  13. Next, the code sets up the screen and obtains the current hour, minute, and second from the system.
  14. The code then sets the position of the turtle for each line in the for a loop.
  15. The first line moves the turtle 200 pixels to the right, 90 pixels down and 70 pixels forward.
  16. The next line moves the turtle again but this time it left 90 pixels to the right and moved it 70 pixels down.
  17. The last two lines move the turtle forward by 200 pixels again and left it at its original position.
  18. Next, the code hides both turtles and waits for a second.
  19. Once a second has passed, it displays time by writing.
RELATED ARTICLES

Most Popular

Recent Comments