python ——钟表
1、表盘
1 from turtle import * 2 from datetime import * 3 4 def Skip(step):#表盘不连续地画图 5 penup() 6 forward(step) 7 pendown() 8 9 def SetupClock(radius): 10 #建立表的外框 11 reset() 12 pensize(7) 13 for i in range(60): 14 Skip(radius)#跨越中间这段不画 15 if i % 5 == 0: 16 forward(20) 17 Skip(-radius-20)#抬起画笔,回到原处 18 else: 19 dot(5) 20 Skip(-radius)#抬起画笔,回到圆心 21 right(6)#回到圆心,方向旋转6度 22 23 SetupClock(160)
2、添加指针
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 #清空窗口,重置默认值 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 goto(0,0) 19 20 def Init(): 21 global secHand, minHand, hurHand, printer 22 mode("logo")# 重置Turtle指向北 23 24 #建立三个表针Turtle并初始化 25 mkHand("secHand", 125) 26 right(30) 27 mkHand("minHand", 110) 28 right(30) 29 mkHand("hurHand", 90) 30 31 secHand = Turtle() 32 secHand.shape("secHand") 33 minHand = Turtle() 34 minHand.shape("minHand") 35 hurHand = Turtle() 36 hurHand.shape("hurHand") 37 38 for hand in secHand, minHand, hurHand: 39 hand.shapesize(1, 1, 3) 40 hand.speed(0) 41 42 #建立输出文字Turtle 43 printer = Turtle() 44 printer.hideturtle() 45 printer.penup() 46 47 #画表框 48 def SetupClock(radius): 49 #建立表的外框 50 reset() 51 pensize(7) 52 for i in range(60): 53 Skip(radius)#跨越中间这段不画 54 if i % 5 == 0: 55 forward(20) 56 Skip(-radius-20)#抬起画笔,回到原处 57 else: 58 dot(5) 59 Skip(-radius)#抬起画笔,回到圆心 60 right(6)#回到圆心,方向旋转6度 61 62 Init() 63 tracer(False) 64 SetupClock(160)
3、时间
1 from datetime import * 2 from turtle import * 3 4 #时分秒 5 t = datetime.today() 6 print(t) 7 print(t.microsecond) 8 print(t.second) 9 second = t.second + t.microsecond*0.000001 10 minute = t.minute + second/60.0 11 hour = t.hour + minute/60.0 12 13 #星期 14 print(t.weekday()) 15 week = ["星期一", "星期二", "星期三", 16 "星期四", "星期五", "星期六", "星期日"] 17 print(week[t.weekday()]) 18 19 #年月日 20 y = t.year 21 m = t.month 22 d = t.day 23 # return "%s %d %d" % (y, m, d) 24 print(y,"-",m,"-",d) 25 26 tracer(False) 27 mode("logo")# 重置Turtle指向北 28 printer = Turtle() 29 printer.forward(65) 30 printer.write(week[t.weekday()], align="center",font=("Courier", 14, "bold"))#日期标在图中 31 printer.back(130) 32 printer.write("%d-%d-%d"%(y,m,d),align="center",font=("Courier", 14, "bold"))#日期标在图中
1 from datetime import * 2 from turtle import * 3 4 #时分秒 5 t = datetime.today() 6 print(t) 7 print(t.microsecond) 8 print(t.second) 9 second = t.second + t.microsecond*0.000001 10 minute = t.minute + second/60.0 11 hour = t.hour + minute/60.0 12 13 #星期 14 print(t.weekday()) 15 week = ["星期一", "星期二", "星期三", 16 "星期四", "星期五", "星期六", "星期日"] 17 print(week[t.weekday()]) 18 19 #年月日 20 y = t.year 21 m = t.month 22 d = t.day 23 # return "%s %d %d" % (y, m, d) 24 print(y,"-",m,"-",d) 25 26 tracer(False) 27 mode("logo")# 重置Turtle指向北 28 printer = Turtle() 29 printer.hideturtle() 30 printer.penup()#提起画笔,便没了那两道轨迹 31 printer.forward(65) 32 printer.write(week[t.weekday()], align="center",font=("Courier", 14, "bold"))#日期标在图中 33 printer.back(130) 34 printer.write("%s-%d-%d"%(y,m,d),align="center",font=("Courier", 14, "bold"))#日期标在图中 35 printer.home() 36 tracer(True)
1 from datetime import * 2 from turtle import * 3 4 def Skip(step):#表盘不连续地画图 5 penup() 6 forward(step) 7 pendown() 8 9 def mkHand(name, length): 10 #注册Turtle形状,建立表针Turtle 11 #清空窗口,重置默认值 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 goto(0,0) 19 20 def Init(): 21 global secHand, minHand, hurHand, printer 22 mode("logo")# 重置Turtle指向北 23 right(90) 24 #建立三个表针Turtle并初始化 25 mkHand("secHand", 125) 26 right(30) 27 mkHand("minHand", 110) 28 right(30) 29 mkHand("hurHand", 90) 30 31 secHand = Turtle() 32 secHand.shape("secHand") 33 minHand = Turtle() 34 minHand.shape("minHand") 35 hurHand = Turtle() 36 hurHand.shape("hurHand") 37 38 for hand in secHand, minHand, hurHand: 39 hand.shapesize(1, 1, 3) 40 hand.speed(0) 41 42 #建立输出文字Turtle 43 printer = Turtle() 44 printer.hideturtle() 45 printer.penup() 46 47 #画表框 48 def SetupClock(radius): 49 #建立表的外框 50 reset() 51 pensize(7) 52 for i in range(60): 53 Skip(radius)#跨越中间这段不画 54 if i % 5 == 0: 55 forward(20) 56 Skip(-radius-20)#抬起画笔,回到原处 57 else: 58 dot(5) 59 Skip(-radius)#抬起画笔,回到圆心 60 right(6)#回到圆心,方向旋转6度 61 62 tracer(False) 63 Init() 64 SetupClock(160) 65 tracer(True) 66 67 #时分秒 68 t = datetime.today() 69 print(t) 70 print(t.microsecond) 71 print(t.second) 72 second = t.second + t.microsecond*0.000001 73 minute = t.minute + second/60.0 74 hour = t.hour + minute/60.0 75 76 77 #星期 78 print(t.weekday()) 79 week = ["星期一", "星期二", "星期三", 80 "星期四", "星期五", "星期六", "星期日"] 81 print(week[t.weekday()]) 82 83 #年月日 84 y = t.year 85 m = t.month 86 d = t.day 87 # return "%s %d %d" % (y, m, d) 88 print(y,"-",m,"-",d) 89 90 tracer(False) 91 #mode("logo")# 重置Turtle指向北 92 printer = Turtle() 93 printer.penup() 94 printer.hideturtle() 95 printer.forward(65) 96 printer.write(week[t.weekday()], align="center",font=("Courier", 14, "bold"))#日期标在图中 97 printer.back(130) 98 printer.write("%d-%d-%d"%(y,m,d), align="center",font=("Courier", 14, "bold"))#日期标在图中 99 tracer(True)
4、动态显示
将时间和角度对应起来,换算到角度,然后和指针对应起来
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", 115) 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(1, 1, 3) 34 hand.speed(0) 35 #建立输出文字Turtle 36 printer = Turtle() 37 printer.hideturtle() 38 printer.penup() 39 40 #画表框 41 def SetupClock(radius): 42 #建立表的外框 43 reset() 44 pensize(7) 45 for i in range(60): 46 Skip(radius)#跨越中间这段不画 47 if i % 5 == 0: 48 forward(20) 49 Skip(-radius-20)#抬起画笔,回到原处 50 else: 51 dot(5) 52 Skip(-radius)#抬起画笔,回到圆心 53 right(6)#回到圆心,方向旋转6度 54 55 def Week(t): 56 week = ["星期一", "星期二", "星期三", 57 "星期四", "星期五", "星期六", "星期日"] 58 return week[t.weekday()] 59 60 def Date(t): 61 y = t.year 62 m = t.month 63 d = t.day 64 return "%s %d %d" % (y, m, d) 65 66 def Tick(): 67 #绘制表针的动态显示 68 t = datetime.today() 69 second = t.second + t.microsecond*0.000001 70 minute = t.minute + second/60.0 71 hour = t.hour + minute/60.0 72 secHand.setheading(6*second) 73 minHand.setheading(6*minute) 74 hurHand.setheading(30*hour) 75 76 tracer(False) 77 printer.forward(65) 78 printer.write(Week(t), align="center", 79 font=("Courier", 14, "bold")) 80 printer.back(130) 81 printer.write(Date(t), align="center", 82 font=("Courier", 14, "bold")) 83 printer.home() 84 85 tracer(True) 86 87 ontimer(Tick, 100)#100ms后继续调用tick 88 89 def main(): 90 tracer(False)#不刷新,最后一次刷新,省时间 91 Init() 92 SetupClock(160) 93 tracer(True) 94 Tick() 95 mainloop() 96 97 if __name__ == "__main__": 98 main()