代码如下:
#(1)七段数码管的绘制.py
#(2)导入库模块
from turtle import *
from random import *
import time
#(3)绘制单段间隔
def drawGap():
penup()
fd(5)
#(4)绘制单段数码管
def drawLine(draw):
drawGap()
if draw:
pendown()
else:
penup()
fd(20)
drawGap()
right(90)
#(5)根据数字绘制七段数码管
def drawDigit(digit):
pencolor(random(),random(),random())
drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False) #用第1段的数字
pencolor(random(),random(),random())
drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False) #用第2段的数字
pencolor(random(),random(),random())
drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False) #用第3段的数字
pencolor(random(),random(),random())
drawLine(True) if digit in [0,2,6,8] else drawLine(False) #用第4段的数字
pencolor(random(),random(),random())
left(90)
drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False) #用第5段的数字
pencolor(random(),random(),random())
drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False) #用第6段的数字
pencolor(random(),random(),random())
drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False) #用第7段的数字
pencolor(random(),random(),random())
left(180)
penup()
fd(20)
#(6)获得要输出的数字
def drawData(date):
for i in date:
if i=='-':
write('年',font=("幼圆",20,"normal"))
fd(30)
elif i=='=':
write('月',font=("楷体",20,"normal"))
fd(30)
elif i=='+':
write('日',font=("华文彩云",20,"normal"))
fd(30)
elif i=='.':
write('时',font=("黑体",20,"normal"))
fd(30)
elif i==',':
write('分',font=("方正姚体",20,"normal"))
fd(30)
elif i==';':
write('秒',font=("方正舒体",20,"normal"))
fd(30)
else:
drawDigit(eval(i))
#(7)主函数(设置画布与画笔等)
def main():
setup(1350,350)
speed(100)
penup()
fd(-500)
pensize(5)
drawData(time.strftime('%Y-%m=%d+%H.%M,%S;',time.localtime()))
hideturtle()
done()
#(8)调用主函数
main()
运行结果如下: