preface
The Python library is numerous and constantly updated, so the most effective way to learn about it is to read the Python documentation. Both Google and Baidu.
Turtle Library Chinese document:
https://docs.python.org/zh-cn/3/library/turtle.html
The development tools
Python version: 3.6.4
Related modules:
Python modules such as the Turtle library.
Environment set up
Install Python and add it to the environment variable.
The principle is introduced
Making a simple clock using Turtle Library can be done in three steps.
Step 1: Initialize
Step 2: Create the clock
The third step is to display the clock dynamically
Initialize four Turtle objects that define the hour hand, minute hand, second hand, and print text. The three Turtle objects, hour hand, minute hand and second hand, are defined as follows:
CreateHand ('minute_hand', 150) CreateHand ('minute_hand', 125) 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)
The createHand function is used to create the watch hand (define the shape length, etc.). The code is implemented as follows:
Turtle "def createHand(name, length): 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)
Then define a Turtle object for printing text:
Printer = Turtle.Turtle() Printer. Hideturtle () Printer. Penup () CreateClock (160) Printer = Turtle.
That is, drawing the clock. Its code is implemented as follows:
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)
To make it easier for you to understand the code, I have recorded a short renderings of this part of the code in operation:
Dynamic display clock source code is as follows:
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. % 12 # Set to Second_Hand. Setheading (6 * second) minute_hand.Setheading (6 * minute) % 12 # Set to Second_Hand. 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, 14) "Bold ")) Printer.Home () Turtle. Tracer (True) # 100ms call once Turtle. OnTimer (lambda: startTick(second_hand, minute_hand, hour_hand, printer), 100)
That is to use the datetime library 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: To render the clock directly when the code is run, the code in steps 1 and 2 sets tracker to False at runtime. Set tracker to True only in the third step.
That’s it for now. Thank you for watching. Follow me for my daily series on Python gadgets
To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.
Dry goods mainly include:
① More than 2000 Python e-books (mainstream and classic books are available)
(2) The Python Standard Library (Chinese version)
③ project source code (40 or 50 interesting and classic hands-on project and source code)
④Python basic introduction, crawler, web development, big data analysis video (suitable for small white learning)
⑤Python Learning Roadmap (Goodbye to Slow Learning)
⑥ Two days of Python crawler training camp live access
All done~ complete source code + dry goods see personal profile or private message to obtain the relevant files.