importtimeimportturtle as t#利用多个小函数,化繁为简#绘制单个数字连接处的小间隔,模块化思维
defdrawgap(ft):
t.penup()
t.fd(0.12*ft)#绘制单个数字的某一条线
defdrawline(e,ft):
drawgap(ft)
t.pendown()if e elset.penup()
t.fd(0.76*ft)
drawgap(ft)
t.right(90)#绘制单个数字,规则化思维
defdraw(digit,ft):
vd=eval(digit)
drawline(True,ft)if vd in [2,3,4,5,6,8,9] elsedrawline(False,ft)
drawline(True,ft)if vd in [0,1,3,4,5,6,7,8,9] elsedrawline(False,ft)
drawline(True,ft)if vd in [0,2,3,5,6,8,9] elsedrawline(False,ft)
drawline(True,ft)if vd in [0,2,6,8] elsedrawline(False,ft)
t.left(90)
drawline(True,ft)if vd in [0,4,5,6,8,9] elsedrawline(False,ft)
drawline(True,ft)if vd in [0,2,3,5,6,7,8,9] elsedrawline(False,ft)
drawline(True,ft)if vd in [0,1,2,3,4,7,8,9] elsedrawline(False,ft)
t.right(180)
t.penup()
t.fd(0.8*ft)#打印汉字前的调整
defprelocation(ft):
t.penup()
t.fd(-10)
t.right(90)
t.fd(1.2*ft)
t.left(90)#打印汉字后的调整
defrelocation(ft):
t.fd(2*ft)
t.left(90)
t.fd(1.2*ft)
t.right(90)#main()函数只负责画图,图的其他设置写在代码中,不占用main()函数,提高main()函数复用性
defmain(ft,fontype,digitnum):
wdsize=int(ft)
t.pensize(0.11 *ft)
t.pencolor('red')for each indigitnum:if each=='+':
prelocation(ft)
t.write('年',font=(fontype,wdsize,'normal'))
relocation(ft)
t.pencolor('green')elif each=='-':
prelocation(ft)
t.write('月',font=(fontype,wdsize,'normal'))
relocation(ft)
t.pencolor('blue')elif each=='=':
prelocation(ft)
t.write('日',font=(fontype,wdsize,'normal'))
relocation(ft)else:
draw(each,ft)#插入计时尾注
deffootnote(dur,ft):
t.right(90)
t.fd(120)
t.left(90)
t.fd(-250)
t.pencolor('gray')
t.write('绘图用时{:.3f}s'.format(dur),font=('SimHei',int(0.4*ft),'italic'))
t.hideturtle()
t.done()#主函数,包括调取系统时间,设置字体大小和类别,展开画布并调整画笔,最后调取main()函数完成制图
digitnum=time.strftime('%Y+%m-%d=',time.gmtime())
foot=46font_type='Arial't.setup(950,350,200,200)
t.penup()
t.fd(-430)
start=time.perf_counter()
main(foot,font_type,digitnum)
interval=time.perf_counter()-start
footnote(interval,foot)