#绘制单端数码管
import turtle
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 drawDate(date):
for i in date:
drawDigit(eval(i))
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.fd(-300)
turtle.pensize(5) #线条粗细
turtle.color("red")
drawDate('20181010')
turtle.hideturtle() #使绘制图面不会立即消失
turtle.done()
main() #调用主函数
改进:
1:获取当前系统时间,绘制对应数码管
2:增加七段数码管之间的间隔线
#七段数码管的绘制
import turtle as t
import time
def drawgap():
t.penup()
t.fd(5)
#绘制单段数码管
def drawline(draw):
t.pendown() if draw else t.penup()
t.fd(40)
drawgap()
t.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)
t.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)
t.left(180)
t.penup()#为绘制后续数字确定位置
t.fd(20)#为绘制后续数字确定位置
#获取日期
def drawdate(date):
t.pencolor("green")
for i in date:
if i == '-':
t.write('年',font = ("Arial",18,"normal"))
t.pencolor("blue")
t.fd(40)
elif i == "=":
t.write('月',font = ("Arial",18,"normal"))
t.pencolor("yellow")
t.fd(40)
elif i == "+":
t.write('日',font = ("Arial",18,"normal"))
t.pencolor("red")
else:
drawdigit(eval(i))#通过eval()将数字变成整数
def main():
t.setup(800,350,200,200)
t.penup()
t.fd(-300)
t.pensize(5)
t.color("red")
drawdate(time.strftime('%Y-%m=%d+', time.gmtime()))#-=+符号分别代表年月日
t.hideturtle()
t.done()
main()#调用main函数
VS安装turtle库方法:
https://blog.csdn.net/qq_45624989/article/details/105717788