七段数码管的绘制

# 七段数码管绘制实例
import turtle as tu
def drawLine(draw):      #绘制单段数码管
    tu.pendown() if draw else tu.penup()
    tu.fd(40)
    tu.right(90)
def drawDight(dight):    #根据数字绘制七段数码管
    drawLine(True) if dight in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if dight in [0, 1, 3, 4, 5, 6, 7, 8 ,9] else drawLine(False)
    drawLine(True) if dight in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if dight in [0, 2, 6, 8] else drawLine(False)
    tu.left(90)
    drawLine(True) if dight in [0, 4, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if dight in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
    drawLine(True) if dight in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
    tu.left(180)
    tu.penup() #为绘制后续数字确定位置
    tu.fd(20) #为绘制后续数字确定位置
def drawDate(date):
    for i in date:
        drawDight(eval(i))
def main():
    tu.setup(800, 350, 200, 200)
    tu.penup()
    tu.fd(-300)
    tu.pensize(5)
    drawDate('2020520')
    tu.hideturtle()
    tu.done()
main()

效果

获取当前时间绘制数码管

# 七段数码管绘制实例
import turtle as tu
import time
def drawGap():           #绘制数码管间隔
    tu.penup()
    tu.fd(5)
def drawLine(draw):      #绘制单段数码管
    drawGap()
    tu.pendown() if draw else tu.penup()
    tu.fd(40)
    drawGap()
    tu.right(90)
def drawDight(dight):    #根据数字绘制七段数码管
    drawLine(True) if dight in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if dight in [0, 1, 3, 4, 5, 6, 7, 8 ,9] else drawLine(False)
    drawLine(True) if dight in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if dight in [0, 2, 6, 8] else drawLine(False)
    tu.left(90)
    drawLine(True) if dight in [0, 4, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if dight in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
    drawLine(True) if dight in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
    tu.left(180)
    tu.penup() #为绘制后续数字确定位置
    tu.fd(20) #为绘制后续数字确定位置
def drawDate(date):     # data为日期,格式为‘%Y-%m=%d+’
    tu.pencolor("red")
    for i in date:
        if i == '-':
            tu.write('年', font=("Arial", 18, "normal"))
            tu.pencolor("green")
            tu.fd(40)
        elif i == '=':
            tu.write('月', font=("Arial", 18, "normal"))
            tu.pencolor("blue")
            tu.fd(40)
        elif i == '+':
            tu.write('日', font=("Arial", 18, "normal"))
        else:
            drawDight(eval(i))
def main():
    tu.setup(800, 350, 200, 200)
    tu.penup()
    tu.fd(-300)
    tu.pensize(5)
    drawDate(time.strftime("%Y-%m=%d+", time.gmtime()))
    tu.hideturtle()
    tu.done()
main()

效果图