preface
Python’s libraries are numerous and constantly being updated, so the most effective way to learn about them is to read the official Python documentation. Using Both Google and Baidu.
Turtle Library
https://docs.python.org/zh-cn/3/library/turtle.html
Copy the code
The development tools
**Python version: **3.6.4
Related modules:
Modules that come with Python such as the Turtle library.
Environment set up
Just install Python and add it to the environment variables.
The principle is introduced
Creating a simple clock with the Turtle library can be done in three steps.
Step 1: Initialize
Step 2: Create a clock
The third step, dynamic display clock
The initialization requires defining four Turtle objects for the hour hand, minute hand, and second hand, as well as for printing text. The three Turtle objects are defined as follows:
createHand('second_hand'.150)
createHand('minute_hand'.125)
createHand('hour_hand'.85)
# seconds, minutes, hours
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)
Copy the code
The createHand function is used to create the hand (define the shape length, etc.), its code implementation is as follows:
Create a hand turtle
def createHand(name, length) :
turtle.reset()
move(-length * 0.01)
turtle.begin_poly()
turtle.forward(length * 1.01)
turtle.end_poly()
hand = turtle.get_poly()
turtle.register_shape(name, hand)
Copy the code
Then define a Turtle object for printing text:
# is used to print text such as dates
printer = turtle.Turtle()
printer.hideturtle()
printer.penup()
createClock(160)
Copy the code
That is, draw the clock. Its code implementation is as follows:
Create a clock
def createClock(radius) :
turtle.reset()
turtle.pensize(7)
for i in range(60):
move(radius)
if i % 5= =0:
turtle.forward(20)
move(-radius-20)
else:
turtle.dot(5)
move(-radius)
turtle.right(6)
Copy the code
To make it easier for you to understand the code, I have recorded a short image of this part of the code in action:
Dynamic display clock source code as follows:
Dynamic display hand
def startTick(second_hand, minute_hand, hour_hand, printer) :
today = datetime.datetime.today()
second = today.second + today.microsecond * 1e-6
minute = today.minute + second / 60.
hour = (today.hour + minute / 60) % 12
# Set orientation
second_hand.setheading(6 * second)
minute_hand.setheading(6 * minute)
hour_hand.setheading(12 * hour)
turtle.tracer(False)
printer.forward(65)
printer.write(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(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)
# 100ms call once
turtle.ontimer(lambda: startTick(second_hand, minute_hand, hour_hand, printer), 100)
Copy the code
The dateTime library is used to obtain the current date and time, the date is printed on both sides of the clock, and according to the time to adjust the Angle of the hand, and indicate the number represented by the point on the clock.
** Note: ** In order to run the code directly with the clock, the code in step 1 and step 2 runs with tracker set to False. Set tracker to True only in step 3.