[Python for Beginners] Example 7: Seven-segment digital tube drawing

Seven-segment digital tube: https://baike.baidu.com/item/Seven-segment digital tube
Such examples are ubiquitous in our lives, and today, we learn how to draw a seven-segment digital tube. Contents used :
the turtle drawing system learned in the early stage, and the practicality of the
function . To draw a seven-segment digital tube, you must first draw a line (a seven-segment digital tube is composed of seven lines). Like this, we decompose a big problem into several small problems, and solve them progressively. It is very common in problem analysis. Then consider drawing a line: due to the different numbers, each of the seven lines is different in brightness and darkness, that is to say, a line has two states: bright and dark. We can write a function to Draws a line and sets its state by giving it a state value.
insert image description here



import turtle as t
def drawline(draw):
    t.pendown() if draw else t.penup()
    t.fd(40)
    t.right(90)

Draw line function: according to the state value draw, to determine pendown (brush down) or penup (brush up), that is, the two states of the line: bright and dark

				![在这里插入图片描述](https://img-blog.csdnimg.cn/20190122121442801.png)

Call this function repeatedly to draw a seven-segment digital tube:

def drawdigit(digit):#根据数值绘制七段数码管
    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)
    t.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 [2,3,4,5,6,8,9] else drawline(False)
    drawline(True) if dight in [0,1,2,3,4,7,8,9] else drawline(False)
    t.left(180)

Seven lines, different numbers have different shades. The parameter digit is a number.
The above has completed the drawing of the first seven-segment digital tube, and then we need to consider the second one. Its initial position should be given after the first drawing is completed, so we add after the above code:

t.pu()
t.fd(20)

A number can be drawn, then a string of numbers can also be drawn:

#七段数码管的绘制
import turtle as t
def drawgap():
    t.pu()
    t.fd(5)
def drawline(draw):#绘制单段数码管
    t.pendown() if draw else t.penup()
    t.fd(40)
    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.pu()#为绘制后续数字确定位置
    t.fd(20)#为绘制后续数字确定位置
def drawdate(date):#获取日期
    for i in date:
        drawdigit(eval(i))#通过eval()将数字变成整数
def main(date):
    t.setup(800,350,200,200)
    t.pu()
    t.fd(-300)
    t.pensize(5)
    t.color("red")
    drawdate(date)
    t.hideturtle()
    t.done()
main(input("请输入一个年月日,与阿拉伯数字形式不加任何标点的形式输入,例:20190122:\n"))

We can optimize this problem. For example, we have learned the use of the time library before, so how about using this library function to obtain the system time and adding the year, month, and day?

#七段数码管的绘制
import turtle as t
def drawgap():
    t.pu()
    t.fd(5)
def drawline(draw):#绘制单段数码管
    t.pendown() if draw else t.penup()
    t.fd(40)
    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.pu()#为绘制后续数字确定位置
    t.fd(20)#为绘制后续数字确定位置
def drawdate(date):#获取日期
    t.pencolor("green")
    for i in date:
        if i == '年':
            t.write('年',font = ("Arial",18,"normal"))
            t.pencolor("blue")
            t.fd(40)
        elif i == "月":
            t.write('月',font = ("Arial",18,"normal"))
            t.pencolor("yellow")
            t.fd(40)
        elif i == "日":
            t.write('日',font = ("Arial",18,"normal"))
            t.pencolor("red")
        else:
            drawdigit(eval(i))#通过eval()将数字变成整数
def main(date):
    t.setup(800,350,200,200)
    t.pu()
    t.fd(-300)
    t.pensize(5)
    t.color("red")
    drawdate(date)
    t.hideturtle()
    t.done()
main(input("请输入一个年月日,例:2019年01月22日:\n"))

insert image description here
Plus system time display:

#七段数码管的绘制
import turtle as t
import time
def drawgap():
    t.pu()
    t.fd(5)
def drawline(draw):#绘制单段数码管
    t.pendown() if draw else t.penup()
    t.fd(40)
    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.pu()#为绘制后续数字确定位置
    t.fd(20)#为绘制后续数字确定位置
def drawdate(date):#获取日期
    t.pencolor("green")
    for i in date:
        if i == '年':
            t.write('年',font = ("Arial",18,"normal"))
            t.pencolor("blue")
            t.fd(40)
        elif i == "月":
            t.write('月',font = ("Arial",18,"normal"))
            t.pencolor("yellow")
            t.fd(40)
        elif i == "日":
            t.write('日',font = ("Arial",18,"normal"))
            t.pencolor("red")
        else:
            drawdigit(eval(i))#通过eval()将数字变成整数
def main(date):
    t.setup(1500,650,20,20)
    t.pu()
    t.fd(-600)
    t.pensize(5)
    drawdate(time.strftime("%Y年%m月%d日",time.gmtime()))
    t.fd(40)
    t.color("red")
    drawdate(date)
    t.hideturtle()
    t.done()
main(input("请输入一个年月日,例:2019年01月22日:\n"))

Perfect!

Guess you like

Origin blog.csdn.net/weixin_43717839/article/details/86593032