Digital Clock using Turtle Python

Introduction

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.

Prerequisites

  • Before starting this project, you should have:

    • Basic knowledge of Python programming
    • An understanding of Python’s turtle module for graphics
    • Familiarity with the datetime module to get the current time

Learning Objectives

  • By completing this project, you will learn how to:

    • Use the turtle module to create graphical applications
    • Work with the datetime module to obtain current system time
    • Implement loops to update and display time continuously

Installation Guide

To run this project on your machine, ensure you have Python installed. Follow these steps:

  1. Download and Install Python: Visit the official Python website to download and install Python if you haven’t already.
  2. Set Up a Python Environment: Use a code editor like Visual Studio Code or an IDE like PyCharm to write and run your code.
  3. Clone or Copy the Code: Copy the project code provided below into a new Python file in your development environment.

Project Steps

  1. Import Required Modules: Import the turtle, time, and datetime modules.
  2. Create Turtle Objects: Initialize two turtles, one for displaying time and another for drawing the clock border.
  3. Set Up the Screen: Create a screen and set its background color.
  4. Draw the Clock Border: Use the second turtle to draw a rectangle that acts as the clock’s border.
  5. Display Time Continuously: Use a loop to continuously update and display the current time.

Code

				
					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
				
			

Code

				
					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
				
			

Pseudo Code explaining this Python Project

				
					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.

				
			

Pseudo Code explaining this Python Project

				
					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.

				
			

Flow Chart

Code Explanation

  • Turtle Graphics: The turtle module is used to create and control a turtle that can draw on the screen. This is used to create the graphical interface of the clock.
  • Time Update: The datetime module provides the current system time, which is used to initialize the clock.
  • Looping for Updates: The while True loop ensures the time is updated every second using time.sleep(1) to pause the execution for one second between updates.

Challenges and Solutions

  • Challenge 1: Keeping Time Accurate

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.

  • Challenge 2: Handling AM/PM Format

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.

Testing

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.

Additional Resources

FAQs

  1. 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.

Project by Nimisha Agrawal and Documented by Aakarsh Pandey, Team edSlash.