七段数码管可以用来表示我们生活中的数字,绘制过程主要有turtle和函数实现,下面是七段数码管的原理
代码如下:
import turtle import time #绘制一条线,并且将turtle转向 def drawLine(draw): # 使每一条线段起末存在间隙 drawGap() turtle.pendown() if draw else turtle.penup() turtle.fd(40) # 使每一条线段起末存在间隙 drawGap() turtle.right(90) #使每一条线段起末存在间隙 def drawGap(): turtle.penup() turtle.fd(5) #绘制一个数 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): for i in date: if i=='N': turtle.pencolor("green") turtle.write('年',font=("Arial",18,"normal")) turtle.fd(40) elif i=='Y': turtle.pencolor("green") turtle.write('月',font=("Arial",18,"normal")) turtle.fd(40) elif i=='R': turtle.pencolor("green") turtle.write('日',font=("Arial",18,"normal")) turtle.fd(40) else: turtle.pencolor("purple") drawDigit(eval(i)) def main(): turtle.setup(800,350,200,200) turtle.penup() turtle.fd(-300) turtle.pensize(5) #获取当前时间并且对时间格式进行标准化 drawDate(time.strftime('%YN%mY%dR',time.gmtime())) turtle.hideturtle() turtle.done() main()
运行结果如下: