编程语言
首页 > 编程语言> > python小实例——七段数码管绘制

python小实例——七段数码管绘制

作者:互联网

首先用time库获取系统当前时间

然后用turtle库画出来

算是对于turtle库内函数的一次实践运用叭

import turtle as t
import time

def drawGap()://因为是数码管所以线条要有空隙
    t.penup()
    t.fd(5)

def drawLine(draw)://画一条线
    drawGap()
    t.pendown() if draw else t.penup()//画不画取决于参数为True还是为False
    t.fd(50)
    drawGap()
    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 drawDate(date):
    t.pencolor('red')

    for i in date:
        if i == '=':
            t.write("年", font=("Arial", 25, "normal"))
            t.pencolor('green')//画笔颜色切换
            t.fd(50)
        elif i == '+':
            t.write("月", font=("Arial", 25, "normal"))
            t.pencolor('blue')
            t.fd(50)
        elif i == '-':
            t.write("日", font=("Arial", 25, "normal"))
            t.fd(50)
        else:
            drawDigit(eval(i))

def main():
    t.setup(1000, 800, 200, 200)
    t.speed(10)
    t.penup()
    t.fd(-400)//刚开始海龟在屏幕正中间,先让往左靠靠
    t.pensize(5)
    drawDate(time.strftime("%Y=%m+%d-", time.gmtime()))//按照自己定义的格式调用系统日期
    t.hideturtle()
    t.done()

main()//最后别忘了调用

效果:

喜欢_月夜 发布了102 篇原创文章 · 获赞 21 · 访问量 1万+ 私信 关注

标签:digit,False,drawLine,python,else,数码管,fd,True,七段
来源: https://blog.csdn.net/weixin_43721423/article/details/104114144