This project involves creating a digital clock using Python’s turtle and datetime modules. The clock will display the current time (hours, minutes, and seconds) and update every second. The graphical interface will be created using the Turtle graphics library, which allows for drawing shapes and handling animations easily.
Before starting this project, you should have:
By completing this project, you will learn how to:
To run this project on your machine, ensure you have Python installed. Follow these steps:
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
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
1. Initialize:
o Import necessary modules (time, datetime, turtle).
o Create two turtle objects, t for displaying time and t1 for drawing the clock border.
2. Set Up Screen:
o Create a screen with a background color.
3. Draw Clock Border:
o Use t1 to draw a rectangle that will serve as the clock's border.
4. Display Time:
o Obtain the current hour, minute, and second.
o Use a loop to update and display the current time every second.
1. Initialize:
o Import necessary modules (time, datetime, turtle).
o Create two turtle objects, t for displaying time and t1 for drawing the clock border.
2. Set Up Screen:
o Create a screen with a background color.
3. Draw Clock Border:
o Use t1 to draw a rectangle that will serve as the clock's border.
4. Display Time:
o Obtain the current hour, minute, and second.
o Use a loop to update and display the current time every second.
Solution: Use time.sleep(1) to pause execution and update the time every second. This ensures the time display is approximately accurate, though slight drifts may occur due to processing time.
Solution: Adjust the hour reset logic to if hr == 24: hr = 0 for 24-hour format. For 12-hour format, use if hr == 13: hr = 1.
To test the clock, simply run the Python script. Observe the time display to ensure it updates every second and transitions correctly between minutes and hours.
Q1: How can I change the background color of the clock?
A1: You can change the background color by modifying the s.bgcolor(“green”) line to any color you prefer.
Q2: Can I display the clock in a 12-hour format?
A2: Yes, you can modify the hour logic in the loop to display hours in a 12-hour format.
Q3: How do I stop the clock?
A3: You can stop the clock by closing the Turtle graphics window.
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India