7段数码管绘制

import turtle as t
import time
def pen():
    t.up()
    t.fd(6)
    t.down()
def drawline(draw):     #绘制单段数码管
    pen()
    t.pendown() if draw else t.penup()  # draw 为真则执行 pendown ,否则执行 penup

    t. fd(40)
    pen()
    t.right(90)
def drawdigit(digit):       #根据数字绘制七段数码管
    drawline(True) if digit in [2,3,4,5,6,8,9] else drawline(False)
    drawline(True) if digit in [0,1,3,4,5,6,7,8,9] else drawline(False)
    drawline(True) if digit in [0,2,3,5,6,8,9] else drawline(False)
    drawline(True) if digit in [0,2,6,8] else drawline(False)
    t.left(90)
    drawline(True) if digit in [0,4,5,6,8,9] else drawline(False)
    drawline(True) if digit in [0,2,3,5,6,7,8,9] else drawline(False)
    drawline(True) if digit in [0,1,2,3,4,7,8,9] else drawline(False)
    t.left(180)
    t.penup()
    t.fd(25)
def drawday(day):       #获取要输出的数字
    for i in day:
        if i == '-':
            t.write('',font = ('隶书',28,'normal'))
            t.pencolor('green')
            t.fd(40)
        elif i == '=':
            t.write('',font = ('隶书',28,'normal'))
            t.pencolor('blue')
            t.fd(40)
        elif i == '+':
            t.write('',font = ('隶书',28,'normal'))
        else:
            drawdigit(eval(i))

def main():
    t.setup(800,350,200,200)
    t.speed(0)
    t.color('red')
    t.hideturtle()#隐藏画笔
    t.penup()
    t.fd(-360)
    t.pensize(5)
    drawday(time.strftime('%H-%M=%S+', time.localtime()))

    t.done()
main()

posted @ 2020-10-18 19:59  山自山  阅读(140)  评论(0编辑  收藏  举报