python编程-17:turtle绘制时钟程序

python编程-17:turtle绘制时钟程序


源码:
  1. from turtle import *
  2. from datetime import *
  3.  
  4. def Skip(step):
  5.     penup()
  6.     forward(step)
  7.     pendown()
  8.  
  9. def mkHand(name, length):
  10.     #注册Turtle形状,建立表针Turtle
  11.     reset()
  12.     Skip(-length*0.1)
  13.     begin_poly()
  14.     forward(length*1.1)
  15.     end_poly()
  16.     handForm = get_poly()
  17.     register_shape(name, handForm)
  18.  
  19. def Init():
  20.     global secHand, minHand, hurHand, printer
  21.     mode("logo")# 重置Turtle指向北
  22.     #建立三个表针Turtle并初始化
  23.     mkHand("secHand"125)
  24.     mkHand("minHand",  130)
  25.     mkHand("hurHand"90)
  26.     secHand = Turtle()
  27.     secHand.shape("secHand")
  28.     minHand = Turtle()
  29.     minHand.shape("minHand")
  30.     hurHand = Turtle()
  31.     hurHand.shape("hurHand")
  32.     for hand in secHand, minHand, hurHand:
  33.         hand.shapesize(113)
  34.         hand.speed(0)
  35.     #建立输出文字Turtle
  36.     printer = Turtle()
  37.     printer.hideturtle()
  38.     printer.penup()
  39.      
  40. def SetupClock(radius):
  41.     #建立表的外框
  42.     reset()
  43.     pensize(7)
  44.     for in range(60):
  45.         Skip(radius)
  46.         if % 5 == 0:
  47.             forward(20)
  48.             Skip(-radius-20)
  49.         else:
  50.             dot(5)
  51.             Skip(-radius)
  52.         right(6)
  53.          
  54. def Week(t):    
  55.     week = ["星期一""星期二""星期三",
  56.             "星期四""星期五""星期六""星期日"]
  57.     return week[t.weekday()]
  58.  
  59. def Date(t):
  60.     = t.year
  61.     = t.month
  62.     = t.day
  63.     return "%s %d %d" % (y, m, d)
  64.  
  65. def Tick():
  66.     #绘制表针的动态显示
  67.     = datetime.today()
  68.     second = t.second + t.microsecond*0.000001
  69.     minute = t.minute + second/60.0
  70.     hour = t.hour + minute/60.0
  71.     secHand.setheading(6*second)
  72.     minHand.setheading(6*minute)
  73.     hurHand.setheading(30*hour)
  74.      
  75.     tracer(False)  
  76.     printer.forward(65)
  77.     printer.write(Week(t), align="center",
  78.                   font=("Courier"14"bold"))
  79.     printer.back(130)
  80.     printer.write(Date(t), align="center",
  81.                   font=("Courier"14"bold"))
  82.     printer.home()
  83.     tracer(True)
  84.  
  85.     ontimer(Tick, 100)#100ms后继续调用tick
  86.  
  87. def main():
  88.     tracer(False)
  89.     Init()
  90.     SetupClock(160)
  91.     tracer(True)
  92.     Tick()
  93.     mainloop()
  94.  
  95. if __name__ == "__main__":       
  96.     main()

原文链接: http://www.icourse163.org/learn/BIT-268001?tid=1002001005#/learn/content?type=detail&id=1002613054&cid=1002856163

你可能感兴趣的:(python编程-17:turtle绘制时钟程序)