Python seven-segment digital tube drawing time

topic

Requirements are as follows To To To

(1) Use the time library to get the current time of the system, the format is as follows: 20190411 To To

(2) Draw the corresponding seven-segment digital tube To To

(3) No limit to digital tube style To To

Source code

Can be directly copied and pasted

import turtle,time
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 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)
    turtle.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)
    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")
        elif i=='+':
            turtle.write("日",font=("Arial",18,"normal"))
        else:
            drawDigit(eval(i))
def main():
    turtle.setup(800,350,200,200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(time.strftime("%Y-%m=%d+",time.gmtime()))
    turtle.hideturtle()
    turtle.done()
main()
    

Reference library

turtle  //进行绘制//
time    //获取系统时间//

Create function

def drawGap():
    turtle.penup()
    turtle.fd(5)

Used for aesthetics to leave a gap between each segment of the digital tube

def drawLine(draw):
    drawGap()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    drawGap()
    turtle.right(90)

If the variable draw is true, drop the pen, otherwise raise the pen to determine whether you need to drop the pen to draw a single number when there are different
numbers

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)
    turtle.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)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)

There are seven segments in the digital tube, and each segment performs digital judgment, and then determines the true or false of the variable draw, and determines whether the pen needs to be dropped. In the last three lines, I want to rotate 180 to the left to ensure that the pen is in the right direction. Lift the pen and advance 20 pixels to the next area.
Judge string one by one

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")
        elif i=='+':
            turtle.write("日",font=("Arial",18,"normal"))
        else:
            drawDigit(eval(i))

Remove the characters from the string one by one.
If the string is "-", output the year in red, and change it to green after output.
If the string is "=", output the month in green, and change it to blue after output.
If the string is "+", the day is output in blue.
Otherwise, use drawDigit to output the corresponding number to
define the main function

def main():
    turtle.setup(800,350,200,200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(time.strftime("%Y-%m=%d+",time.gmtime()))
    turtle.hideturtle()
    turtle.done()

First define the size of the canvas, lift up the brush, and move 300 to the left until it reaches the left side of the canvas (can be adjusted according to the canvas). Set the brush thickness to five. Get the system time and assign it to the variable in the form of "%Y-%m=%d+". The special symbols here must be the same as the one above. Secondly, when painting is over, the brush is hidden, and finally it ends.
The above is the definition of all required functions, and the defined function system does not run. Finally, compile the main function

main()

operation resultInsert picture description here

Guess you like

Origin blog.csdn.net/weixin_50835854/article/details/112811255