一、问题描述
如下所示的七段数码管绘制时间(年月日)
二、代码实现
"""
七段数码管绘制年月日时间
"""
import turtle
import time
def drawLine(draw):
# 绘制单端数码管
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
turtle.right(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)
turtle.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)
turtle.left(180)
turtle.penup()
turtle.fd(20)
# 为绘制后续数字确定起始位置
def drawData(data):
turtle.pencolor("red")
for i in str(data):
if i == "-":
turtle.write("年",font=("Arial",18,"normal"))
turtle.pencolor("green")
turtle.fd(40)
elif i == "=":
turtle.write("月",font=("Arial",18,"normal"))
turtle.pencolor("blue")
turtle.fd(40)
elif i == "+":
turtle.write("日",font=("Arial",18,"normal"))
else:
drawDigit(eval(i))
def main():
turtle.setup(800,350,200,200)
# 画布大小
turtle.penup()
turtle.fd(-300)
turtle.pensize(5)
data = time.strftime("%Y-%m=%d+",time.gmtime())
drawData(data)
turtle.hideturtle()
# 隐藏turtle(即箭头)
turtle.done()
# 给用户时间来查看图形,没有这行,图形窗口会在程序完成是立即关闭。
main()