zoukankan      html  css  js  c++  java
  • 封装得很好的七段数码管Python turtle绘图

    7段数码管Python绘图,获取用time获取系统时间,用turtle将年月日打印出来,计算打印所需时间.代码如下:

    import time
    import turtle as t
    
    #利用多个小函数,化繁为简
    #绘制单个数字连接处的小间隔,模块化思维
    def drawgap(ft):
        t.penup()
        t.fd(0.12*ft)
    
    #绘制单个数字的某一条线
    def drawline(e,ft):
        drawgap(ft)
        t.pendown() if e else t.penup()
        t.fd(0.76*ft)
        drawgap(ft)
        t.right(90)
    
    #绘制单个数字,规则化思维
    def draw(digit,ft):
        vd=eval(digit)
        drawline(True,ft) if vd in [2,3,4,5,6,8,9] else drawline(False,ft)
        drawline(True,ft) if vd in [0,1,3,4,5,6,7,8,9] else drawline(False,ft)
        drawline(True,ft) if vd in [0,2,3,5,6,8,9] else drawline(False,ft)
        drawline(True,ft) if vd in [0,2,6,8] else drawline(False,ft)
        t.left(90)
        drawline(True,ft) if vd in [0,4,5,6,8,9] else drawline(False,ft)
        drawline(True,ft) if vd in [0,2,3,5,6,7,8,9] else drawline(False,ft)
        drawline(True,ft) if vd in [0,1,2,3,4,7,8,9] else drawline(False,ft)
        t.right(180)
        t.penup()
        t.fd(0.8*ft)
    
    #打印汉字前的调整
    def prelocation(ft):
        t.penup()
        t.fd(-10)
        t.right(90)
        t.fd(1.2*ft)
        t.left(90)
    
    #打印汉字后的调整
    def relocation(ft):
        t.fd(2*ft)
        t.left(90)
        t.fd(1.2*ft)
        t.right(90)
    
    #main()函数只负责画图,图的其他设置写在代码中,不占用main()函数,提高main()函数复用性
    def main(ft,fontype,digitnum):
        wdsize=int(ft)
        t.pensize(0.11 * ft)
        t.pencolor('red')
        for each in digitnum:
            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)
    
    #插入计时尾注
    def footnote(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=46
    font_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)

    输出如下,很美丽的数码管!

    下篇博客扩展一下,设计一个倒计时显示!

  • 相关阅读:
    混合知识点
    源码系列--OkHttp(CallServerInterceptor)
    源码系列--OkHttp(2)
    源码系列--OkHttp
    java代码编译得到smali代码
    C++入门笔记
    Flutter入门(五)--表单+单选/多选+日期+轮播+对话框
    Flutter入门(四)--顶部导航+侧边栏+按钮
    Flutter入门(三)-底部导航+路由
    Flutter入门(二)--布局
  • 原文地址:https://www.cnblogs.com/zhangziyan/p/9191586.html
Copyright © 2011-2022 走看看