七段数码管的绘制
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0d30867c49f7119899e249058ac5cce2.png#pic_center)
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()
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e2b3709c84bc7431f4cbd06e3f1f0fcc.png)
获取当前时间绘制数码管
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()
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0354ff0a1bbf6a01b048ae13bc2f223e.png)