///最近在学习Python,一道小的课后练习,利用turtle 库绘制七段数码管,并显示系统当前日期。想到之前单片机中的数码管驱动,采用解码方式绘制七段码。输入任意一个日期,如20170623,打印该日期。
#Seven_SEG_Data_Show
from turtle import Turtle
#绘制数码管的一段,x,y是起始坐标
def draw_line(pen,x,y,n):
if n==0:
pen.penup()
pen.goto(x,y+2)
pen.left(90)
pen.pendown()
pen.forward(50)
pen.right(90)
elif n==1:
pen.penup()
pen.goto(x,y+56)
pen.left(90)
pen.pendown()
pen.forward(50)
pen.right(90)
elif n==2:
pen.penup()
pen.goto(x+2,y+108)
pen.pendown()
pen.forward(50)
elif n==3:
pen.penup()
pen.goto(x+54,y+106)
pen.right(90)
pen.pendown()
pen.forward(50)
pen.left(90)
elif n==4:
pen.penup()
pen.goto(x+54,y+52)
pen.pendown()
pen.right(90)
pen.forward(50)
pen.left(90)
elif n==5:
pen.penup()
pen.goto(x+2,y)
pen.pendown()
pen.forward(50)
elif n==6:
pen.penup()
pen.goto(x+2,y+54)
pen.pendown()
pen.forward(50)
#将数字解码为对应的七段码
def number_decode(num):
if num==0:
seven_seg=[1,1,1,1,1,1,0]
elif num==1:
seven_seg=[0,0,0,1,1,0,0]
elif num==2:
seven_seg=[1,0,1,1,0,1,1]
elif num==3:
seven_seg=[0,0,1,1,1,1,1]
elif num==4:
seven_seg=[0,1,0,1,1,0,1]
elif num==5:
seven_seg=[0,1,1,0,1,1,1]
elif num==6:
seven_seg=[1,1,1,0,1,1,1]
elif num==7:
seven_seg=[0,0,1,1,1,0,0]
elif num==8:
seven_seg=[1,1,1,1,1,1,1]
elif num==9:
seven_seg=[0,1,1,1,1,1,1]
return seven_seg
#绘制一个七段码数字
def draw_num(seven_seg,x,y,color):
pen=Turtle()
pen.hideturtle()
pen.color(color)
pen.pensize(3)
pen.speed(100)
sequence=0
for i in seven_seg:
if i==1:
draw_line(pen,x,y,sequence)
sequence=sequence+1
#日期显示
def dateShow(date,color):
x_location=-260
for i in date:
decode_num=number_decode(int(i))
draw_num(decode_num,x_location,0,color)
x_location=x_location+70
def main():
date=input("please input date number:")
dateShow(date,"red")
main()