(Python)实例7:七段数码管绘制

 背景

数码管是一种价格便宜、使用简单的发光电子器件,广泛应用在价格较低的电子类产品中,其中,七段数码管最为常用。七段数码管( Seven- segment Indicator)由7段数码管拼接而成,每段有亮或不亮两种情况. 本节通过turtle库函数绘制七段数码管形式的日期信息。

该问题的IPO 描述如下:

输入: 当前日期的数字形式

处理: 根据每个数字绘制七段数码管表示

输出: 绘制当前日期的七段数码管表示 

 

绘制起点在数码管的中部左侧,按照如图所示的顺序来涂画。无论每段数码管是否被画出来,turtle画笔都要按顺序“画完”7个数码管。如果某数字的数码管在某段不亮,则经过该段前应将画笔抬起,等到某段亮的时候再落下绘画。

纯数字版

import turtle,datetime
def drawLine(draw):
    turtle.pendown() if draw else turtle.penup()    #绘制单端数码管
    turtle.fd(40)
    turtle.right(90)
def drawDight(d):   #根据数字绘制七段数码管
    drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False)
    drawLine(True) if d in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if d in [0, 2, 6, 8] else drawLine(False)
    turtle.left(90) #???
    drawLine(True) if d in [0, 4, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if d in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
    drawLine(True) if d in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)
def drawDate(date):     #获得想要输出的数字
    for i in date:
        drawDight(eval(i))
def main():
    turtle.setup(800,350,200,200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(datetime.datetime.now().strftime('%Y%m%d'))
    turtle.hideturtle() 
main()
turtle.done()  

turtle.left(90) 把上行代码函数中的右转抵消,下一行应该直行。

这里的 turtle.hideturtle() 作用是运行完后把海龟图标藏起来

最后的 turtle.done() 是运行后不让画布立即消失。

运行结果:

 

数字+单位版

import turtle,datetime
def drawGap():
    turtle.penup()
    turtle.fd(5)
def drawLine(draw):
    drawGap()
    turtle.pendown() if draw else turtle.penup()    #绘制单端数码管
    turtle.fd(40)
    drawGap()
    turtle.right(90)
def drawDight(d):   #根据数字绘制七段数码管
    drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False)
    drawLine(True) if d in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if d in [0, 2, 6, 8] else drawLine(False)
    turtle.left(90) #把上行代码函数中的右转抵消
    drawLine(True) if d in [0, 4, 5, 6, 8, 9] else drawLine(False)
    drawLine(True) if d in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
    drawLine(True) if d in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)
def drawDate(date):     #获得想要输出的数字
    turtle.pencolor("red")
    for i in date:
        if i=='-':
            turtle.write('年',font=("Arial",18,"normal"))    
            turtle.pencolor("green")
            turtle.fd(40)
        elif i=='=':
            turtle.write('月',font=("Arial",18,"normal"))
            turtle.pencolor("blue")
            turtle.fd(40)
        elif i =='+':
            turtle.write('日',font=("Arial",18,"normal"))
            # turtle.pencolor("green")
            # turtle.fd(40)
        else:
            drawDight(eval(i))
def main():
    turtle.setup(800,350,200,200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
    turtle.hideturtle() #画完后将海龟图标藏起来
main()
turtle.done()   #让画布不消失
font=("Arial",18,"normal")

表示使用Arial字体,字体大小为18,字体样式为正常。

drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))

代码中,%Y表示年份,%m表示月份,%d表示日期。

遇到‘-’打出“年”,遇到‘=’打出“年”,遇到‘+’打出“年”。

这里又添加了不同的颜色的画笔,红色的写年份,绿色的写月份,蓝色的写日期。

运行结果:

 

除此之外,你也可以写出时间比如:小时,分钟,秒数,星期等等,赶快去试试吧! 

 

作者:宸冬_

物联沃分享整理
物联沃-IOTWORD物联网 » (Python)实例7:七段数码管绘制

发表回复