How To Create Analog Clock in Python using Turtle

Create Analog Clock in Python using Turtle

In this tutorial, we will learn how to Create Analog Clock in Python using Turtle module. Analog clocks are also called the wall clock.

Before we start learning to Create Analog Clock in Python using Turtle, be sure that you know the Turtle module basics because we will be using it everywhere in the code along with the Python language. If you don’t know how to draw something with Python’s turtle module or if you need some help, then click here to get help anytime.

Now, we are ready to start with confidence, so let’s start.


First, we will import the required modules.

import turtle
import time

Then, we will set some parameters like creating a screen, setting the background color, setting up geometry, title, and tracer of our turtle window.

screen = turtle.Screen()
screen.bgcolor("white")
screen.setup(width=600, height=600)
screen.title("Ghadi")
screen.tracer(0)

Then, we will create/initalize the turtle module

kalam = turtle.Turtle()

We will then hide the turtle/pen

kalam.hideturtle()

Now, we will set the speed of the turtle using speed() function

kalam.speed(0)

We will also set the size of the pen/turtle using pensize() function

kalam.pensize(3)


Then we have created a function(ghadi_bana) which will be used to create/draw the clock, here we will see the code only, we will understand it using comments later.

def ghadi_bana(ghantaa, minutee, secondd, kalam):
    kalam.up()
    kalam.goto(0, 210)
    kalam.setheading(180)
    kalam.color("red")
    kalam.pendown()
    kalam.circle(210)

    kalam.up()
    kalam.goto(0, 0)
    kalam.setheading(90)

    for z in range(12):
        kalam.fd(190)
        kalam.pendown()
        kalam.fd(20)
        kalam.penup()
        kalam.goto(0, 0)
        kalam.rt(30)

    hands = [("black", 80, 12), ("black", 150, 60), ("black", 110, 60)]
    time_set = (ghantaa, minutee, secondd)    
    for hand in hands:
        time_part = time_set[hands.index(hand)]
        angle = (time_part/hand[2])*360
        kalam.penup()
        kalam.goto(0, 0)
        kalam.color(hand[0])
        kalam.setheading(90)
        kalam.rt(angle)
        kalam.pendown()
        kalam.fd(hand[1])

Now, we will create a while loop which will handle the hands of our Analog Clock. We will be creating three hands as a wall clock have for hours, minutes, and seconds. Check the code below:

while True:
    ghantaa = int(time.strftime("%I"))
    minutee = int(time.strftime("%M"))
    secondd = int(time.strftime("%S"))
    ghadi_bana(ghantaa, minutee, secondd, kalam)
    screen.update()
    time.sleep(1)
    kalam.clear()

Now, let’s see the complete code, and we will use the comments to understand our “Create Analog Clock in Python using Turtle” project in more details.


Code To Create Analog Clock in Python using Turtle


import turtle
import time

screen = turtle.Screen() #turtle screen
screen.bgcolor("white") #background of the screen
screen.setup(width=600, height=600) #geometry of the GUI
screen.title("Ghadi") #title of the GUI
screen.tracer(0) #tracer for the GUI

kalam = turtle.Turtle() #the turtle
kalam.hideturtle() #make the turtle invisible
kalam.speed(0) #setting the speed to 0
kalam.pensize(3) #setting the pensize to 3


def ghadi_bana(ghantaa, minutee, secondd, kalam): #function with 4 parameters

    kalam.up() #not ready to draw
    kalam.goto(0, 210) #positioning the turtle
    kalam.setheading(180) #setting the heading to 180
    kalam.color("red") #setting the color of the pen to red
    kalam.pendown() #starting to draw
    kalam.circle(210) #a circle with the radius 210

    kalam.up() #not ready to draw
    kalam.goto(0, 0) #positioning the turtle
    kalam.setheading(90) #same as seth(90) in newer version

    for z in range(12): #loop 
        kalam.fd(190) #moving forward at 190 units
        kalam.pendown() #starting to draw
        kalam.fd(20) #forward at 20
        kalam.penup() #not ready to draw
        kalam.goto(0, 0) #positioning the turtle
        kalam.rt(30) #right at an angle of 30 degrees

    hands = [("black", 80, 12), ("black", 150, 60), ("black", 110, 60)] #the color and the hands set
    time_set = (ghantaa, minutee, secondd) #setting the time

    for hand in hands: #loop
        time_part = time_set[hands.index(hand)] #time part in the hand index in hands of time_Set
        angle = (time_part/hand[2])*360 #setting the angle for the clock
        kalam.penup() #not ready to draw
        kalam.goto(0, 0) #positioning the turtle
        kalam.color(hand[0]) #setting the color of the hand
        kalam.setheading(90) #same as seth(90)
        kalam.rt(angle) #right at an angle of "right"
        kalam.pendown() #ready to draw
        kalam.fd(hand[1]) #forward at a unit of 1st index of the hand var


while True:
    ghantaa = int(time.strftime("%I")) #setting the hour from the time module
    minutee = int(time.strftime("%M")) #setting the minute from the time module
    secondd = int(time.strftime("%S")) #setting the second as above

    ghadi_bana(ghantaa, minutee, secondd, kalam) #calling the ghanta_bana() function with the given parameters
    screen.update() #updating the scren
    time.sleep(1) #making the code sleep for a second with the time module
    kalam.clear() #clearing the pen

Output:



Create Analog Clock in Python using Turtle
Create Analog Clock in Python using Turtle

Thank you for reading till the end.

Keep Learning, Keep Coding




If you found something wrong in the article, let us know.


Also Read:


  • Drawing Application in Python Tkinter
    Introduction In this article, we will design and construct a basic Drawing Application in Python Tkinter GUI, where we can simply draw something on the canvas using a pencil and erase with an eraser, as well as the ability to change the thickness of a pencil and eraser. We may also modify the canvas’s background…
  • Draw Flag of USA using Python Turtle
    Introduction Welcome everyone to copyassignment.com . In this tutorial, we are going to draw the Flag of USA using Python Turtle. The python turtle module is very easy to understand and learn. The python beginners can easily grasp the turtle functions as we are providing the comments and a detailed explanation of the code. Complete…
  • Draw Iron Man Face with Python Turtle: Tony Stark Face
    Introduction This blog will discuss how to draw Iron Man Face with Python Turtle, a GUI-based 2D graphics module in Python. The popular Marvel Cinematic Universe character Iron Man is played by the talented actor Robert Downey Jr. In this article, we will use sketchpy library to draw his face in just 3 lines of…
  • Draw TikTok Logo with Python Turtle
    Introduction This blog will discuss drawing TikTok Logo with Python Turtle, a GUI-based 2D graphics module in Python. TikTok is a trendy social media platform, primarily used for uploading and editing short dance videos, but has now become a funny, educational, and artistic video. Basics of the Python Turtle module: https://copyassignment.com/the-beginners-guide-to-python-turtle/ Official Documentation of the…
  • Draw Instagram Logo using Python Turtle
    Introduction This blog will discuss how to Draw Instagram Logo using Python Turtle module. Instagram is a very popular social media platform, primarily used for uploading and editing photos in the past, but it has now become a short-video platform with the introduction of Instagram Reels. Click here to check how to draw Instagram Reels…
  • I Love You Text in ASCII Art
    I Love You Text in ASCII Art This is simple I LOVE YOU Text in ASCII Art, you can use the button to copy this TEXT in ASCII Art and paste it into your system. Heart with Arrow Text in ASCII Art This is a simple Heart with Arrow Text in ASCII Art, you can…
  • Python Turtle Shapes- Square, Rectangle, Circle
    We all know that Python offers a variety of libraries for game Development, GUI Development, Data Visualization, and many more. These libraries not only make our work easy but also make the code efficient and deployable. Keeping this thing in mind, today, in this article, we will discuss and learn about Graphics Programming by using…
  • Python Turtle Commands and All Methods
    Introduction In this tutorial, we are going to learn about the python turtle commands and methods which we are using in the python turtle programming where Turtle is a python library that is used in creating games, pictures, and graphic images. If you have used Python Turtle a little bit, you may have observed that…
  • Happy Birthday Python Program In Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to write the Happy Birthday Python Program In Turtle. Let’s say you want to wish Happy Birthday to someone with your Python skills, in that case, the code below can help you to wish Happy Birthday with Python Program In Turtle. Complete code for Happy…
  • I Love You Program In Python Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to write the I Love You Program In Python Turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Click here if you want…
  • Draw Python Logo in Python Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to Draw Python Logo in Python Turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Step 1: Importing Libraries Step 2: Creating a…
  • Space Invaders game using Python
    We have already developed multiple games using the turtle library and now continuing our series of projects development in Python using the turtle library, today, in this article, we are going to develop a Space Invaders game using Python.As we all know the number of various built-in functions that a turtle library offers is more…
  • Draw Google Drive Logo Using Python
    Introduction: Hello and welcome to the copyassignment where we will learn how to Draw Google Drive Logo Using Python turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Well, Google Drive is a…
  • Draw Instagram Reel Logo Using Python
    Introduction Today’s topic will be how to Draw Instagram Reel Logo Using Python Turtle. Hey guys, tell us something that we all use in our daily lives. Someone wants to know how the Instagram reel logo was created. Can we create this logo in the simplest and most basic programming language, Python? Definitely! We can…
  • Draw The Spotify Logo in Python Turtle
    Introduction If you want to learn how to draw the Spotify logo in Python Turtle, you’ve come to the right place. Today, in this tutorial, I will show you how to draw the Spotify logo in Python Turtle with code, so stick with me until the end. We begin by importing the turtle module. Then…
  • Draw The CRED Logo Using Python Turtle
    Introduction Hello Folks, Welcome to copyassignment.com. In this tutorial, we’ll look at creating a CRED Logo Using Python Turtle. This will be a very short but fascinating article, especially for newbies, because we have commented on every line of code so that even a beginner can understand the concept. To create a CRED logo, we…
  • Draw Javascript Logo using Python Turtle
    Introduction Hello, and welcome friends to learn a new topic today. We are going to draw the Javascript Logo using a python turtle module. It’s super easy and exciting to learn as we have used simple lines of code. It is understandable to all the beginners interested in learning python. We provide all the explanations…
  • Draw Dell Logo using Python Turtle
    Introduction Hello everyone, welcome to copyassignment.com. In this article, we are going to learn to draw a dell logo using python turtle module. This is a very simple and easy design that is easily understandable to all python beginners. The code is explained line by line for easy understanding. The comments are provided in the…
  • Draw Spider web using Python Turtle
    Introduction Hello friends, today in this tutorial we are going to learn how to draw a Spider web using the Python Turtle module. The code is very simple, interesting, and easy to understand. We are providing the comments and detailed explanations for easy understanding of the code. For the complete code go to the bottom…
  • Draw Chrome Logo using Python Turtle
    Introduction Hello and welcome friends to copyassignment.com.  We all know the chrome browser very well as most of our surfing activities are done on chrome. We also look at the Chrome logo every now and then while browsing. But have you ever tried to draw the Logo? In this article, we are going to do…

Share:
Avatar of Harry

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you