Python-turtle库绘制蚊香、走动的时钟和数码管日期

源码:

1 import turtle    #导入turtle库,turtle库是python的基础绘图库
2 
3 turtle.pensize(30)    #设置画笔粗细
4 for i in range(30):
5     turtle.circle(i*10,60)    #循环画圆30次,每次画60度并且半径增加10,也就是说画了30×60度的圆形,1800度,即5圈
6 turtle.done()    #画完之后,使面板停留,否则画笔画完将自动关闭

 

效果图:

在这里插入图片描述

源码:

 1 # -*- coding:utf-8 –*-
 2 # 用turtlr画时钟
 3 # 以自定义shape的方式实现
 4 import turtle as t
 5 import datetime as d
 6 def skip(step):  # 抬笔,跳到一个地方
 7     t.penup()
 8     t.forward(step)
 9     t.pendown()
10 def drawClock(radius):  # 画表盘
11     t.speed(0)
12     t.mode("logo")  # 以Logo坐标、角度方式
13     t.hideturtle()
14     t.pensize(7)
15     t.home()  # 回到圆点
16     for j in range(60):
17         skip(radius)
18         if (j % 5 == 0):
19             t.forward(20)
20             skip(-radius - 20)
21         else:
22             t.dot(5)
23             skip(-radius)
24         t.right(6)
25 def makePoint(pointName, len):  # 钟的指针,时针、分针、秒针
26     t.penup()
27     t.home()
28     t.begin_poly()
29     t.back(0.1 * len)
30     t.forward(len * 1.1)
31     t.end_poly()
32     poly = t.get_poly()
33     t.register_shape(pointName, poly)  # 注册为一个shape
34 def drawPoint():  # 画指针
35     global hourPoint, minPoint, secPoint, fontWriter
36     makePoint("hourPoint", 100)
37     makePoint("minPoint", 120)
38     makePoint("secPoint", 140)
39     hourPoint = t.Pen()  # 每个指针是一只新turtle
40     hourPoint.shape("hourPoint")
41     hourPoint.shapesize(1, 1, 6)
42     minPoint = t.Pen()
43     minPoint.shape("minPoint")
44     minPoint.shapesize(1, 1, 4)
45     secPoint = t.Pen()
46     secPoint.shape("secPoint")
47     secPoint.pencolor('red')
48     fontWriter = t.Pen()
49     fontWriter.pencolor('gray')
50     fontWriter.hideturtle()
51 def getWeekName(weekday):
52     weekName = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
53     return weekName[weekday]
54 def getDate(year, month, day):
55     return "%s-%s-%s" % (year, month, day)
56 def realTime():
57     curr = d.datetime.now()
58     curr_year = curr.year
59     curr_month = curr.month
60     curr_day = curr.day
61     curr_hour = curr.hour
62     curr_minute = curr.minute
63     curr_second = curr.second
64     curr_weekday = curr.weekday()
65     t.tracer(False)
66     secPoint.setheading(360 / 60 * curr_second)
67     minPoint.setheading(360 / 60 * curr_minute)
68     hourPoint.setheading(360 / 12 * curr_hour + 30 / 60 * curr_minute)
69     fontWriter.clear()
70     fontWriter.home()
71     fontWriter.penup()
72     fontWriter.forward(80)
73     # 用turtle写文字
74     fontWriter.write(getWeekName(curr_weekday), align="center", font=("Courier", 14, "bold"))
75     fontWriter.forward(-160)
76     fontWriter.write(getDate(curr_year, curr_month, curr_day), align="center", font=("Courier", 14, "bold"))
77     t.tracer(True)
78     print(curr_second)
79     t.ontimer(realTime, 100)  # 每隔100毫秒调用一次realTime()
80 def main():
81     t.tracer(False)
82     drawClock(160)
83     drawPoint()
84     realTime()
85     t.tracer(True)
86     t.mainloop()
87 if __name__ == '__main__':
88     main()

 

效果图:

在这里插入图片描述

源码:

 1 import turtle as t
 2 import datetime
 3 def drawGap(): #绘制数码管间隔
 4     t.penup()
 5     t.fd(5)
 6 def drawLine(draw):   #绘制单段数码管
 7     drawGap()
 8     t.pendown() if draw else t.penup()
 9     t.fd(40)
10     drawGap()
11     t.right(90)
12 def drawDigit(d): #根据数字绘制七段数码管
13     drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
14     drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
15     drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
16     drawLine(True) if d in [0,2,6,8] else drawLine(False)
17     t.left(90)
18     drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
19     drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
20     drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
21     t.left(180)
22     t.penup()
23     t.fd(20)
24 def drawDate(date):
25     t.pencolor("red")
26     for i in date:
27         if i == '-':
28             t.write('', font=("Arial", 18, "normal"))
29             t.pencolor("green")
30             t.fd(40)
31         elif i == '=':
32             t.write('', font=("Arial", 18, "normal"))
33             t.pencolor("blue")
34             t.fd(40)
35         elif i == '+':
36             t.write('', font=("Arial", 18, "normal"))
37         else:
38             drawDigit(eval(i))
39 def main():
40     t.setup(800, 350, 200, 200)
41     t.penup()
42     t.fd(-350)
43     t.pensize(5)
44     drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
45     t.hideturtle()
46     t.exitonclick()
47 main()

 

效果图:

在这里插入图片描述

在这里插入图片描述

posted @ 2021-02-16 09:59  BugMiaowu2021  阅读(436)  评论(0编辑  收藏  举报