python drawline_封装得很好的七段数码管Python turtle绘图

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值