python实现七段数码管显示
运行原理:如图所示,七段数码管由七个线条组成,可以有固定顺序(1--7),不同数字显示不同的线条
1)绘制单个数字
def drawline(draw):#绘制单段数码管函数
turtle.pendown()if draw else turtle.penup()#如果有就落笔,没有就抬笔
turtle.fd(40)#绘制
turtle.right(90)#顺时针转90度继续
def drawdigit(digit):#根据数字绘制七段数码管
drawline(True) if digit in [2,3,4,5,6,8,9] else drawline(False)#判断第1段数码管(如果数字为2345689则需要绘制第一笔)
drawline(True) if digit in [0,1,3,4,5,6,7,8,9] else drawline(False)#判断第2段数码管
drawline(True) if digit in [0,2,3,5,6,8,9] else drawline(False)#判断第3段数码管
drawline(True) if digit in [0,2,6,8] else drawline(False)#判断第4段数码管
turtle.left(90)#走完1234段数码管,到第5段时需直走,在原来right90的基础上恢复过来就left90
drawline(True) if digit in [0,4,5,6,8,9] else drawline(False)#判断第5段数码管(如果数字为045689则需要绘制第一笔)
drawline(True) if digit in [0,2,3,5,6,7,8,9] else drawline(False)#判断第6段数码管
drawline(True) if digit in [0,1,2,3,4,7,8,9] else drawline(False)#判断第7段数码管
2)绘制多个(如:获得当前系统时间)
#七段数码管绘制(时间)
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)#顺时针转90度继续
def drawdigit(digit):#根据数字绘制七段数码管
drawline(True) if digit in [2,3,4,5,6,8,9] else drawline(False)#判断第1段数码管(如果数字为2345689则需要绘制第一笔)
drawline(True) if digit in [0,1,3,4,5,6,7,8,9] else drawline(False)#判断第2段数码管
drawline(True) if digit in [0,2,3,5,6,8,9] else drawline(False)#判断第3段数码管
drawline(True) if digit in [0,2,6,8] else drawline(False)#判断第4段数码管
turtle.left(90)#走完1234段数码管,到第5段时需直走,在原来right90的基础上恢复过来就left90
drawline(True) if digit in [0,4,5,6,8,9] else drawline(False)#判断第5段数码管(如果数字为045689则需要绘制第一笔)
drawline(True) if digit in [0,2,3,5,6,7,8,9] else drawline(False)#判断第6段数码管
drawline(True) if digit in [0,1,2,3,4,7,8,9] else drawline(False)#判断第7段数码管
turtle.left(180)#调整方向,让下一个绘制回归原点
turtle.penup()#抬笔
turtle.fd(20)#前进20,为后续数字确定位置
def drawdate(date):#日期处理,格式为‘%y+%m-%d=’
for i in date:
if i=='+':
turtle.write('年',font=("Arial",25,"normal"))
turtle.fd(40)
elif i=='-':
turtle.write('月',font=("Arial",25,"normal"))
turtle.fd(40)
elif i=='=':
turtle.write('日',font=("Arial",25,"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()
3)实现倒计时(10秒)
import turtle
def drawline(draw):
turtle.pendown()if draw else turtle.penup()
turtle.fd(40)
turtle.right(90)
def drawdigit(digit):
i=0
while i<len(digit):
if digit[i]>='0'and digit[i]<='9':
drawline(True) if eval(digit[i]) in [2,3,4,5,6,8,9] else drawline(False)
drawline(True) if eval(digit[i]) in[0,1,3,4,5,6,7,8,9] else drawline(False)
drawline(True) if eval(digit[i]) in [0,2,3,5,6,8,9] else drawline(False)
drawline(True) if eval(digit[i]) in [0,2,6,8] else drawline(False)
turtle.left(90)
drawline(True) if eval(digit[i]) in [0,4,5,6,8,9] else drawline(False)
drawline(True) if eval(digit[i]) in [0,2,3,5,6,7,8,9] else drawline(False)
drawline(True) if eval(digit[i]) in [0,1,2,3,4,7,8,9] else drawline(False)
turtle.clear()#清屏
turtle.left(180)
turtle.penup()
turtle.fd(-40)#复位
else:
break
i=i+1
def main():
turtle.setup(800,350,200,200)
turtle.pensize(5)
drawdigit('9876543210')
turtle.hideturtle()
turtle.done()
main()