显示要求:
i代'd码实u现:
#引入turtle库
import turtle as tu
#引入time库
import time
#定义绘制数码管间隔函数,即每段之间要有间隔
def drawGap():
tu.penup()
tu.fd(10)
#定义画线函数
def drawLine(draw):
drawGap() #定义每段数码管的间隔
tu.pendown() if draw else tu.penup() #控制画笔起落
tu.fd(40)
drawGap()
tu.right(90) #向右转90度
#定义数码管函数
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) #此时回到了起点
tu.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)
tu.left(180)
tu.penup()
tu.fd(20)
#定义所画数据的函数
def drawDate(date): #data为日期,格式为'%Y-%m=%d+'
tu.pencolor("red")
for i in date:
if i == "-":
tu.write('年', font=("Arial", 18, "normal")) #格式规范
tu.pencolor("green")
tu.fd(40)
elif i == '=':
tu.write('月', font=("Arial", 18, "normal"))
tu.pencolor("blue")
tu.fd(40)
elif i == '+':
tu.write('日', font=("Arial", 18, "normal"))
else:
drawDigit(eval(i))
#定义主函数调用这写函数
def main():
tu.setup(1000, 350, 200, 200)
tu.penup()
tu.fd(-300)
tu.pensize(5)
drawDate(time.strftime("%Y-%m=%d+", time.gmtime())) #time中的strftime函数对时间进行格式化
tu.hideturtle()
tu.done()
#执行main函数
main()