Language: Python
IDE:Python.IDE
Write a clock program that requires dynamic updates based on time
Code Ideas
Requirements: 5 Turtle objects, 1 drawn outer plates + 3 analog table pins + 1 Output text
STEP1: Set up Turtle object and initialize
STEP2: Static dial Drawing
STEP3: Update hands position and time information according to clock
Basic library: Turtle, datetime
Code Snippets
From turtle Import * to datetime import * def Skip (step): Penup () forward (step) Pendown () def mkhand (name, Length): #注册Turtle形状, set up hands Turtle reset () Skip (-length*0.1) Begin_poly () forward (length*1.1) End_po Ly () Handform = Get_poly () #注册Turtle形状命令register_shape (Name,shape=none) register_shape (name, Handform) def in
It (): Global Sechand, Minhand, Hurhand, printer mode ("logo") # Reset Turtle point to North #建立三个表针Turtle并初始化 #第二个参数为长度 Mkhand ("Sechand",) Mkhand ("Minhand", 130) Mkhand ("Hurhand",) Sechand = Turtle () Sechand.shape ("sec Hand ") Minhand = Turtle () minhand.shape (" Minhand ") Hurhand = Turtle () hurhand.shape (" Hurhand ") for Han D in Sechand, Minhand, HurHand:hand.shapesize (1, 1, 3) hand.speed (0) #建立输出文字Turtle printer = Turt Le () printer.hideturtle () printer.penup () def setupclock (RADIUS): #建立表的外框 reset () pensize (7) for
I in range (60):Skip (RADIUS) if I% 5 = 0:forward Skip (-radius-20) Else:dot (5) Skip (-radius) Right (6) def Week (t): Week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "star Period six "," 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) Minhand.setheadin G (6*minute) hurhand.setheading (30*hour) #介入Tracer函数以控制刷新速度 Tracer (False) Printer.forward (print) Er.write (Week (t), align= "Center" ("Courier", "The Bold") Printer.back (130) Printer.write ( Date (t), align= "Center" ("Courier", "The Bold") Printer.home () tracer (True) OnTimer ( Tick #100ms后继续调用ticK def Main (): Tracer (False) Init () Setupclock (160) Tracer (True) Tick () Mainloop () if __name__ = = "__main__": Main ()