使用turtle库,time库自动绘制七段数码管,显示当前日期
#画笔粗细2.5,绘制结束隐藏画笔,线段间隔3/6,导入时间模块,手动输入日期改为自动获取日期
import turtle as t
import time
#定义画线or不画线函数
def DrawLine(draw):
t.pu()
t.fd(3)
t.pd()
t.pd() if draw else t.pu()
t.fd(40)
t.pu()
t.fd(3)
t.pd()
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,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,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.pu()
t.left(180)
t.fd(20)
#定义年月日输出函数
def write_data(s):
t.color((255,177,77))
t.write(s,font=("C:\\windows:\\font:\\msyh.ttl","10"))
t.pu()
t.fd(20)
t.pd()
t.color((89,89,89))
#获取时间数据
#定义绘制函数
def Draw(data):
for digit in data:
if digit == '-':
write_data("年")
elif digit == '=':
write_data("月")
elif digit == '+':
write_data("日")
else:
digit=eval(digit)
DrawDigit(digit)
t.hideturtle()
t.done()
#定义主函数
def main():
t.pensize(2.5)
t.pu()
t.goto(-300,0)
t.colormode(255)
t.color((89,89,89))
#引入时间
Draw(time.strftime('%Y-%m-%d+',time.gmtime()))
#调用主函数
main()