The Wayback Machine - https://web.archive.org/web/20241005092445/https://www.geeksforgeeks.org/create-digital-clock-using-python-turtle/
Open In App

Create digital clock using Python-Turtle

Last Updated : 25 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Previous Article
Next Article

Similar Reads

Draw Clock Design using Turtle in Python
Prerequisites: Turtle Programming in Python Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move turtle, there are some functions i.e forward(), backward(), etc. To draw Clock Design : Following steps are used : Import turtl
2 min read
Turtle Race Game Using Python - Turtle Graphics Library
Turtle graphics is a popular way to introduce programming concepts to beginners. It's a fun and interactive module in Python that lets you create simple drawings and animations using a "turtle" that moves around the screen. In this tutorial, we'll create an exciting turtle race game where you can bet on the color of a turtle and watch as they race
4 min read
Python | Create a digital clock using Tkinter
As we know Tkinter is used to create a variety of GUI (Graphical User Interface) applications. In this article we will learn how to create a Digital clock using Tkinter. Prerequisites: Python functions Tkinter basics (Label Widget) Time module https://www.youtube.com/watch?v=1INA9AmaDtQ Using Label widget from Tkinter and time module: In the follow
2 min read
turtle.forward() method in Python-Turtle
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.forward() The turtle.forward() method is used to move the turtle forward by the value of the argument that it takes. It gives
1 min read
turtle.setpos() and turtle.goto() functions in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.setpos() This method is used to move the turtle to an absolute position. This method has Aliases: setpos, setposition, goto. S
1 min read
PyQt5 - Create a digital clock
In this article we will see how to create a digital clock using PyQt5, this digital clock will basically tells the current time in the 24 hour format. In order to create a digital clock we have to do the following: Create a vertical layoutCreate label to show the current time and put it in the layout and align it to the center.Create a QTimer objec
3 min read
Python | Create a stopwatch using clock object in kivy using .kv file
Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Clock Object: The Clock object allows you to schedule a function call in
6 min read
Python | Create a stopwatch Using Clock Object in kivy
Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications.In this, we are going to see how can we create a stopwatch using a label. In the code, we will be creating just a c
4 min read
Create Analog Clock using PyQt5 in Python
Prerequisite: Introduction to pyqt-5 A clock or watch is called "analog" when it has moving hands and (usually) hours marked from number 1 to 12 to show you the time. Some have Roman Numerals (I, II, III, etc) instead, or no numbers at all! In other words: not a digital clock. PyQt5 is a cross-platform GUI toolkit, a set of python bindings for Qt v
3 min read
Create Health Clock for programmers using Python
In this article, we will be creating a Python script that takes care of the health of a person, especially a programmer. It will remind the user to drink water, do an eye exercise, and do a physical exercise at different specified time intervals. It will also keep a log of time at which the exercises are done, water is drunk as well as the no of ex
9 min read
Create a Simple Two Player Game using Turtle in Python
Prerequisites: Turtle Programming in Python TurtleMove game is basically a luck-based game. In this game two-players (Red & Blue), using their own turtle (object) play the game. How to play The game is played in the predefined grid having some boundaries. Both players move the turtle for a unit distance.Now both players flip the coin:if HEAD, t
4 min read
Create pong game using Python - Turtle
Pong is one of the most famous arcade games, simulating table tennis. Each player controls a paddle in the game by dragging it vertically across the screen's left or right side. Players use their paddles to strike back and forth on the ball. Turtle is an inbuilt graphic module in Python. It uses a panel and pen to depict illustrations. Below are th
3 min read
Create a Snake-Game using Turtle in Python
A snake game is an arcade maze game which has been developed by Gremlin Industries and published by Sega in October 1976. It is considered to be a skillful game and has been popularized among people for generations. The snake in the Snake game is controlled using the four direction buttons relative to the direction it is headed in. The player's obj
8 min read
Create an Alarm Clock using Tkinter
Prerequisite: Python GUI – tkinter, winsound, time, and datetime. As we all know, nowadays wake up on time is a very difficult task. The solution is the Alarm Clock. In this article, we will learn, How to create an Alarm Clock using Tkinter in Python. Without alarms, several people would oversleep and land up late for work. Alarm clocks may also be
4 min read
How to Create custom Turtle shapes in Python?
In Turtle, by default, we have an arrowhead-shaped cursor for drawing on the canvas. This can be changed to some other predefined shape or we can also create a custom shape and register it under a name. Not just that, we can even use gif format images to replace our cursor. Changing cursor to predefined shapes The shape() function is used to set th
2 min read
Clock App with Kivy using Python
In this article, we are going to see how to develop a Clock Application with Kivy using Python. Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mouse and keyboard inputs, it also supports multitouc
4 min read
Draw Circle in Python using Turtle
Turtle is a Python feature like a drawing board, which lets us command a turtle to draw all over it! We can use functions like turtle.forward(â€Ķ) and turtle.right(â€Ķ) which can move the turtle around. Turtle is a beginner-friendly way to learn Python by running some basic commands and viewing the turtle do it graphically. It is like a drawing board t
3 min read
Draw a Hut using turtle module in Python
Turtle is a inbuilt module in Python, which has many functions like forward(), backward(), right() and left() etc. You can draw a hut on the screen just by using the turtle module in Python. In this article, we will create a hut using the turtle module. Approach: Import turtleSet the background color.Define a function to draw the front portion of t
2 min read
Python - Hilbert Curve using turtle
Fractal is a curve or a figure which repeats itself. It comprises a recursive pattern that repeats itself up to a desired level of nesting. Turtle graphics are provided in the turtle module which is used for drawing various shapes and patterns in Python. A Hilbert curve is a curve that is formed by connecting a sequence of U-shaped curves arranged
4 min read
Y Fractal tree in Python using Turtle
A fractal is a never-ending pattern. Fractals are infinitely complex patterns that are self-similar across different scales. They are created by repeating a simple process over and over in an ongoing feedback loop. Driven by recursion, fractals are images of dynamic systems – the pictures of Chaos. In this article, we will draw a colorful Y fractal
2 min read
Draw a Tic Tac Toe Board using Python-Turtle
The Task Here is to Make a Tic Tac Toe board layout using Turtle Graphics in Python. For that lets first know what is Turtle Graphics. Turtle graphics In computer graphics, turtle graphics are vector graphics using a relative cursor upon a Cartesian plane. Turtle is drawing board like feature which let us to command a turtle and draw using it. Feat
2 min read
Draw Cube and Cuboid in Python using Turtle
Prerequisite: Turtle Programming Basics Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move the turtle, there are some functions i.e forward(), backward(), etc. Drawing Cube Following steps are used: First draw the front sq
2 min read
Draw Shape inside Shape in Python Using Turtle
Prerequisites: Turtle Programming in Python Turtle is a Python feature like a drawing board, which let us command a turtle to draw all over it! We can use many turtle functions which can move the turtle around. Turtle comes in the turtle library. The turtle module can be used in both object-oriented and procedure-oriented ways. Some of the commonly
3 min read
Draw Colored Solid Cube using Turtle in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move this turtle (pen) and to move the turtle(pen), there are some functions like the forward(), backward(), etc. Prerequisite: Turtle Programming Basics Drawing Colored Solid Cube In this section, we will
2 min read
Draw smiling face emoji using Turtle in Python
Prerequisite: Python Turtle Basics Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle. To move turtle, there are some functions i.e forward(), backward(), etc. In this article, we will see how to draw a smiling face emoji using the Turtl
2 min read
Draw Concentric Circles with VIBGYOR Using Turtle in Python
Prerequisite: Turtle Programming Basics Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move turtle, there are some functions i.e forward(), backward(), etc. To draw Concentric VIBGYOR : Following steps are used : Importing
2 min read
Colored Flower by circles using Turtle in Python
Prerequisite: Turtle Programming Basics Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move turtle, there are some functions i.e forward(), backward(), etc. To draw Flower : Following steps are used : Import TurtleSet scree
1 min read
Draw Panda Using Turtle Graphics in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move the turtle (pen), and to move the turtle, there are some functions like the forward(), backward(), etc. Prerequisite: Turtle Programming Basics Draw Panda Using Turtle Graphics In this section, we wil
2 min read
Draw Heart Using Turtle Graphics in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move the turtle (pen) and to move the turtle, there are some functions like the forward(), backward(), etc Prerequisite: Turtle Programming Basics Draw Heart Using Turtle Graphics In this section, we will
2 min read
Draw Rainbow using Turtle Graphics in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move the turtle (pen), and to move the turtle, there are some functions like the forward(), backward(), etc. Prerequisite: Turtle Programming Basics Draw Rainbow Using Turtle Graphics In this section, we w
2 min read
Article Tags :
Practice Tags :