Python libraries are numerous and constantly updated, so the most efficient way to learn these libraries is to read the official Python documentation. At the same time with the help of Google and Baidu.
The official document address for the Turtle Library described in this article is: https://docs.python.org/3.5/library/turtle.html
the basic ideas for drawing dynamic clocks are as follows (Object-oriented programming):
Use 5 Turtle objects
1 Turtle: Draw outer Disc
3 Turtle: Simulating hands behavior
1 Turtle: Output on the dial on the text
Using the OnTimer () function to update the Dial screen based on real-time time, the display effect is as follows:
The use of related functions is commented in detail in the program, and the code is as follows:
From turtle Import * to datetime import * def Skip (step): Penup () forward (step) Pendown () def mkhand (Nam E, length): #注册Turtle形状, set up hands Turtle reset () #清空当前窗口, reset the position and other information for the default value Skip (-length*0.1) begin_poly () Forwar D (length*1.1) end_poly () Handform = Get_poly () register_shape (name, handform) def Init (): Global Sechan D, Minhand, Hurhand, printer mode ("logo") # Reset Turtle point North #建立三个表针Turtle并初始化 mkhand ("Sechand", 135) Mkhand ("Mi Nhand "," Mkhand ("Hurhand") Sechand = Turtle () sechand.shape ("Sechand") Minhand = Turtle () min
Hand.shape ("Minhand") Hurhand = Turtle () hurhand.shape ("Hurhand") for Hand in Sechand, Minhand, Hurhand: Hand.shapesize (1, 1, 3) hand.speed (0) #建立输出文字Turtle printer = Turtle () printer.hideturtle () PRI Nter.penup () def setupclock (RADIUS): #建立表的外框 reset () pensize (7) for I in range: Skip (rad IUS) If I% 5 = = 0:forward Skip (-radius-20) Else:dot (5) Skip (-radius) Right (6) def Week (t): Week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] return Week[t.weekday ()] def Date (t): y = t.year m = T.month D = t.day return "%s%d%d"% (Y, M, d) def Tick (): #绘制表针的动态显示 t = datetime.today () Second = T.second + t.microsecond*0.000001 minute = T.minute + second/ 60.0 hour = T.hour + minute/60.0 sechand.setheading (6*second) #设置朝向, rotate 6 degrees per second minhand.setheading (6*minute) Hu Rhand.setheading (30*hour) tracer (False) #不显示绘制的过程, direct display of plot results Printer.forward (printer.write) Week (t), a lign= "Center" ("Courier", "bold")) Printer.back (130) Printer.write (Date (t), align= "CEN"
Ter "," ("Courier", "bold")) Printer.back printer.write ("I_chaoren", align= "center", ("Courier", "bold") Printer.home () tracer (True) OnTimer (Tick, 1000) #1000ms后继续调用tick def main (): TR Acer (False) #使多个绘制对象同时显示 Init () setupclock (160) Tracer (True) Tick () Mainloop () If __name__ = "__mai
N__ ": Main ()