时钟小程序(复制大佬代码)
Posted 康栋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了时钟小程序(复制大佬代码)相关的知识,希望对你有一定的参考价值。
链接:https://www.cnblogs.com/nowgood/p/turtle.html
1 # coding=utf-8 2 3 import turtle 4 from datetime import * 5 6 # 抬起画笔,向前运动一段距离放下 7 def Skip(step): 8 turtle.penup() 9 turtle.forward(step) 10 turtle.pendown() 11 12 def mkHand(name, length): 13 # 注册Turtle形状,建立表针Turtle 14 turtle.reset() 15 Skip(-length * 0.1) 16 # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。 17 turtle.begin_poly() 18 turtle.forward(length * 1.1) 19 # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。 20 turtle.end_poly() 21 # 返回最后记录的多边形。 22 handForm = turtle.get_poly() 23 turtle.register_shape(name, handForm) 24 25 def Init(): 26 global secHand, minHand, hurHand, printer 27 # 重置Turtle指向北 28 turtle.mode("logo") 29 # 建立三个表针Turtle并初始化 30 mkHand("secHand", 135) 31 mkHand("minHand", 125) 32 mkHand("hurHand", 90) 33 secHand = turtle.Turtle() 34 secHand.shape("secHand") 35 minHand = turtle.Turtle() 36 minHand.shape("minHand") 37 hurHand = turtle.Turtle() 38 hurHand.shape("hurHand") 39 40 for hand in secHand, minHand, hurHand: 41 hand.shapesize(1, 1, 3) 42 hand.speed(0) 43 44 # 建立输出文字Turtle 45 printer = turtle.Turtle() 46 # 隐藏画笔的turtle形状 47 printer.hideturtle() 48 printer.penup() 49 50 def SetupClock(radius): 51 # 建立表的外框 52 turtle.reset() 53 turtle.pensize(7) 54 for i in range(60): 55 Skip(radius) 56 if i % 5 == 0: 57 turtle.forward(20) 58 Skip(-radius - 20) 59 60 Skip(radius + 20) 61 if i == 0: 62 turtle.write(int(12), align="center", font=("Courier", 14, "bold")) 63 elif i == 30: 64 Skip(25) 65 turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) 66 Skip(-25) 67 elif (i == 25 or i == 35): 68 Skip(20) 69 turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) 70 Skip(-20) 71 else: 72 turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) 73 Skip(-radius - 20) 74 else: 75 turtle.dot(5) 76 Skip(-radius) 77 turtle.right(6) 78 79 def Week(t): 80 week = ["星期一", "星期二", "星期三", 81 "星期四", "星期五", "星期六", "星期日"] 82 return week[t.weekday()] 83 84 def Date(t): 85 y = t.year 86 m = t.month 87 d = t.day 88 return "%s %d%d" % (y, m, d) 89 90 def Tick(): 91 # 绘制表针的动态显示 92 t = datetime.today() 93 second = t.second + t.microsecond * 0.000001 94 minute = t.minute + second / 60.0 95 hour = t.hour + minute / 60.0 96 secHand.setheading(6 * second) 97 minHand.setheading(6 * minute) 98 hurHand.setheading(30 * hour) 99 100 turtle.tracer(False) 101 printer.forward(65) 102 printer.write(Week(t), align="center", 103 font=("Courier", 14, "bold")) 104 printer.back(130) 105 printer.write(Date(t), align="center", 106 font=("Courier", 14, "bold")) 107 printer.home() 108 turtle.tracer(True) 109 110 # 100ms后继续调用tick 111 turtle.ontimer(Tick, 100) 112 113 def main(): 114 # 打开/关闭龟动画,并为更新图纸设置延迟。 115 turtle.tracer(False) 116 Init() 117 SetupClock(160) 118 turtle.tracer(True) 119 Tick() 120 turtle.mainloop() 121 122 if __name__ == "__main__": 123 main()
待校赛结束后研究
以上是关于时钟小程序(复制大佬代码)的主要内容,如果未能解决你的问题,请参考以下文章