代码比较长。
引入库:
from turtle import*
from random import*
绘制单段数码管:
import time
import turtle
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):
pencolor(random () , random () , random ())
drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine (False)
pencolor(random () , random () , random ())
drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine (False)
pencolor(random (), random () , random ())
drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine (False)
pencolor(random (), random (), random ())
drawLine(True) if digit in [0,2,6,8] else drawLine (False)
pencolor(random (), random () , random ())
left(90)
drawLine(True) if digit in [0,4,5,6,8,9] else drawLine (False)
pencolor(random () , random () , random ())
drawLine(True) if digit in [0,2,3,5.6,7,8,9] else drawLine (False)
pencolor (random () , random () , random ())
drawLine (True) if digit in [0,1,2,3,4,7,8,9] else drawLine (False)
pencolor (random () , random () , random ())
left(180)
penup()
fd(20)
获得要输出的数字:
def drawData (date):
pencolor ("red")
for i in date:
if i=='-':
write('年',font=("楷体",20,"normal"))
pencolor ("green")
fd(40)
elif i=='=':
write('月',font=("楷体",20,"normal"))
pencolor ("blue")
fd(40)
elif i=='+':
write('日',font=("楷体",20,"normal"))
else:
drawDigit(eval(i))
主函数:
def main():
setup(800,350,200,200)
penup()
fd(-300)
pensize(5)
#drawData (input ('请输入您要绘制的日期:'))
drawData(time.strftime ('%Y-%m=%d+',time.localtime()))
hideturtle()
done()
运行结果:
试试看吧。