导航

7段数码管绘制

Posted on 2022-12-08 18:56  寻找内啡肽  阅读(93)  评论(0编辑  收藏  举报
# 绘制七段数码管
import turtle as t
import time


def drawGap():      # 绘制数码管间隔的函数
    t.penup()
    t.fd(5)


def drawLine(draw):     # 单个线段的绘制
    drawGap()       # 在线段绘制前,分隔一段距离
    t.pendown() if draw else t.penup()
    t.fd(40)
    drawGap()       # 在线段绘制后,分隔一段距离
    t.right(90)
    return


def drawDigit(digit):       # 数字0-9对应的七段数码管的绘制
    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(20)


def drawDate(date):     # 给定日期转化为数字,画出每个数字
    t.pencolor("red")
    for i in date:
        if i == "-":
            t.write("", font=("Arial", 18, "normal"))
            t.pencolor("green")
            t.fd(40)
        elif i == "=":
            t.write("", font=("Arial", 18, "normal"))
            t.pencolor("blue")
            t.fd(40)
        elif i == "+":
            t.write("", font=("Arial", 18, "normal"))
        
        elif i == '/':
            t.write('', font=("Arial", 18, "normal"))
            t.pencolor('green')
            t.fd(40)
        elif i == '!':
            t.write('', font=("Arial", 18, "normal"))
            t.pencolor('blue')
            t.fd(40)
         
        elif i == '@':
            t.write('', font=("Arial", 18, "normal"))
            t.pencolor('black')
            t.fd(40)
        elif i == '$':
            t.write('星期', font=("Arial", 18, "normal"))
            t.pencolor('purple')
            t.fd(50)
        else:
            drawDigit(eval(i))
    t.exitonclick()
        
def main():
    t.setup(1366, 350, 0, 200)
    t.penup()
    t.fd(-600)
    t.pensize(5)
    drawDate(time.strftime("%Y-%m=%d+%H/%M!%S@$", time.gmtime()))
    t.hideturtle()
    t.done()


main()