七段数码管的绘制
import turtle as tu
def drawLine(draw):
tu.pendown() if draw else tu.penup()
tu.fd(40)
tu.right(90)
def drawDight(dight):
drawLine(True) if dight in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
drawLine(True) if dight in [0, 1, 3, 4, 5, 6, 7, 8 ,9] else drawLine(False)
drawLine(True) if dight in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
drawLine(True) if dight in [0, 2, 6, 8] else drawLine(False)
tu.left(90)
drawLine(True) if dight in [0, 4, 5, 6, 8, 9] else drawLine(False)
drawLine(True) if dight in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
drawLine(True) if dight in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
tu.left(180)
tu.penup()
tu.fd(20)
def drawDate(date):
for i in date:
drawDight(eval(i))
def main():
tu.setup(800, 350, 200, 200)
tu.penup()
tu.fd(-300)
tu.pensize(5)
drawDate('2020520')
tu.hideturtle()
tu.done()
main()
效果
获取当前时间绘制数码管
import turtle as tu
import time
def drawGap():
tu.penup()
tu.fd(5)
def drawLine(draw):
drawGap()
tu.pendown() if draw else tu.penup()
tu.fd(40)
drawGap()
tu.right(90)
def drawDight(dight):
drawLine(True) if dight in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
drawLine(True) if dight in [0, 1, 3, 4, 5, 6, 7, 8 ,9] else drawLine(False)
drawLine(True) if dight in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
drawLine(True) if dight in [0, 2, 6, 8] else drawLine(False)
tu.left(90)
drawLine(True) if dight in [0, 4, 5, 6, 8, 9] else drawLine(False)
drawLine(True) if dight in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
drawLine(True) if dight in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
tu.left(180)
tu.penup()
tu.fd(20)
def drawDate(date):
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:
drawDight(eval(i))
def main():
tu.setup(800, 350, 200, 200)
tu.penup()
tu.fd(-300)
tu.pensize(5)
drawDate(time.strftime("%Y-%m=%d+", time.gmtime()))
tu.hideturtle()
tu.done()
main()
效果图