We have used the turtle module to create an awesome analog clock. This is a step-by-step tutorial on how to create an analog clock using the Python module for graphics named Turtle.
Turtle is a fun project created for making graphics with Python Programming Langauge. A complete tutorial of the turtle module should be better to read before deep-diving into the Project. The complete Source code of this Project as Available here. You can copy the code and practice it step by step.
In this turtle Tutorial, I will walk you through each step to learn an analog clock with Python. To learn turtle while creating a project will be no less than fun. Let's get started now.
Create analog clock with Turtle module
Before deep-diving let's first see what we are going to create in this tutorial. Check out the following picture, in this picture you see a fully functional analog clock created with Python Module turtle.
import turtle
import datetime
class Clock():
tool_name = 'Clock'
def __init__(self, title='Clock', time_deltas=(0, 0, 0), **kwargs):
turtle.title(title)
self.time_deltas = time_deltas
def move(self, distance):
turtle.penup()
turtle.forward(distance)
turtle.pendown()
def createhand(self, name, length):
turtle.reset()
self.move(-length * 0.01)
turtle.begin_poly()
turtle.forward(length * 1.01)
turtle.end_poly()
hand = turtle.get_poly()
turtle.register_shape(name, hand)
def createclock(self, radius):
turtle.reset()
turtle.pensize(7)
for i in range(60):
self.move(radius)
if i % 5 == 0:
turtle.forward(20)
self.move(-radius-20)
else:
turtle.dot(5)
self.move(-radius)
turtle.right(6)
def getweekday(self, today):
return [ 'Mon', 'Tue', 'Wed', 'Thirs', 'Fir', 'Sat','Sun'][today.weekday()]
def getdate(self, today):
return '%s:%s:%s' % (today.year, today.month, today.day)
def starttick(self, second_hand, minute_hand, hour_hand, printer):
today = datetime.datetime.today()
second = today.second + self.time_deltas[0] + today.microsecond * 1e-6
minute = today.minute + self.time_deltas[1] + second / 60.
hour = (today.hour + self.time_deltas[2] + minute / 60) % 12
# 设置朝向
second_hand.setheading(6 * second)
minute_hand.setheading(6 * minute)
hour_hand.setheading(30 * hour)
turtle.tracer(False)
printer.forward(65)
printer.write(self.getweekday(today), align='center', font=("Courier", 14, "bold"))
printer.forward(120)
printer.write('12', align='center', font=("Courier", 14, "bold"))
printer.back(250)
printer.write(self.getdate(today), align='center', font=("Courier", 14, "bold"))
printer.back(145)
printer.write('6', align='center', font=("Courier", 14, "bold"))
printer.home()
printer.right(92.5)
printer.forward(200)
printer.write('3', align='center', font=("Courier", 14, "bold"))
printer.left(2.5)
printer.back(400)
printer.write('9', align='center', font=("Courier", 14, "bold"))
printer.home()
turtle.tracer(True)
turtle.ontimer(lambda: self.starttick(second_hand, minute_hand, hour_hand, printer), 100)
def run(self):
turtle.tracer(False)
turtle.mode('logo')
self.createhand('second_hand', 150)
self.createhand('minute_hand', 125)
self.createhand('hour_hand', 85)
second_hand = turtle.Turtle()
second_hand.shape('second_hand')
minute_hand = turtle.Turtle()
minute_hand.shape('minute_hand')
hour_hand = turtle.Turtle()
hour_hand.shape('hour_hand')
for hand in [second_hand, minute_hand, hour_hand]:
hand.shapesize(1, 1, 3)
hand.speed(0)
printer = turtle.Turtle()
printer.hideturtle()
printer.penup()
self.createclock(160)
turtle.tracer(True)
self.starttick(second_hand, minute_hand, hour_hand, printer)
turtle.mainloop()
clock =Clock()
clock.run()
How to create this Analog Clock with turtle
We have used a few basic methods of the turtle module. If you want to learn how to create these functions, you can always have a look at this Easy to Follow Turtle Tutorial with an Example.
Resources to Learn Python Programming
Python is an easy-to-learn programming language. If you are into python programming you can follow the following tutorial to learn about different Python Modules
- Python zlib Module - Examples and Use
- 14 Best Python Libraries for Image Processing
- 28+ Python GUI Development Libraries or Frameworks
- 3 Python Libraries for Working With PDF
- 4 awesome Python Libraries for Cryptography
- 6 Best Python Libraries for Data Analysis
- 7 Unique Python Libraries for File Manipulation
- 9 Python Libraries for working with Microsoft Office Document
- 9 Unexplored Python Libraries for DateTime
- Create Analog Clock with Python Turtle Module - New!
- Create Python Executable with zipapp Python Module
- Create Sounds With Python Package Pydub
- Create an Amazing Art With Python Turtle - Pickachu Character - New!
- Creating Button with Tkinter Python | Creating GUI with Python
- Encode and decode uuencoded files with uu module python
- Encoding and Decoding XDR data with Python
- File System with python-fsutil | A complete Guide
- Free Python PDF Download : Clean Architectures in Python
- How do I Open Web Browser in Python
- How to Change WiFi Password With Python
- How to Compress a file to zip format with Python
- How to Convert a String into DateTime in Python | AlixaProdev
- How to Extract a Zip File With Python
- How to Generate Temporary Files With Python
- How to Learn Python Programming in 2022
- How to Perform Operation on Audio with Python
- How to Search a File in All Folders with Python
- How to Use DateTime package in Python
- How to Use Python Glob Module
- How to Use Requests-HTML Library in Python
- How to check if an Email Address is Valid and Exists With Python
- How to install requests Library In Python?
- How to make a GET Request in Python
- How to raise a warning in Python - Python warnings module Examples
- How to read XML Document with Python
- How to read XML file With Python
- How to use Hashlib Python- A Complete Guide
- How to use Python OS Module?
- How to wrap a text with Python - textwrap module - New!
- Learn Numpy in 5 Minutes | AlixaProDev
- Make a Server With Python - Server Examples
- Play Beep sound with Python
- Python Module unicodedata - unicode Database
- Python The xml.dom Module
- Python Turtle Project - Create Doraemon Character - New!
- Python threading module - Guide to create thread - New!
- Python urllib module - A complete Guide to URL handling with Python
- Python zipfile module - Comprehensive Examples
- Python zlib.compress(data, /, level=-1)
- Python zlib.decompress(data)
- Python: Working with Windows Registry
- Read Audio File With Python wave Module
- Socket Programming with Python | All you should know about socket Programming
- The Python sys Module - New!
- The Simplest way to find runtime of Python Code - New!
- Top 10 Most Popular Python Web Frameworks
- Top 13 Python Libraries for Data Visualization
- Top 13 Python Libraries for manipulating Audio
- Top 3 Python Libraries for video Editing
- Top 4 Python Libraries for Data Structure and Algorithm
- Top 5 Python Libraries for Downloading media
- Top 6 Python Libraries for sending emails
- Top 7 Python Libraries for Deep Learning
- Top 7 Python libraries for Computer Vision
- Ultimate Guide to Python turtle module - Create Graphics with Python - New!
- Using time module more Effectively - Python time Module - New!
- Write "Hello World" With tkinter Python | Creating GUI with Python
- how to use python termios module - New!
- tarfile module Python - Read and write Archive files with Python
- weakref Module — Weak references Examples in Python
- zoneinfo module in python - IANA time zone