#SevenDigitsDrawV2.py
import turtle as t
import time
DrawMode=0
def drawGap(): #绘制数码管间隔
t.penup()
t.fd(5)
def drawLine(draw): #绘制单段数码管
drawGap()
if DrawMode==0:
t.pendown() if draw else t.penup()
else:
t.penup() if draw else t.pendown()
#t.pencolor("gray")
t.fd(40)
drawGap()
t.right(90)
def drawDigit(d): #根据数字绘制七段数码管
drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,2,6,8] else drawLine(False)
t.left(90)
drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
t.left(180)
t.penup()
t.fd(20)
def drawDate(date):
for i in date:
if i == '-':
if DrawMode==0:
t.pencolor("cyan")
t.write('年',font=("Arial", 18, "normal"))
t.fd(40)
elif i == '=':
if DrawMode==0:
t.write('月',font=("Arial", 18, "normal"))
#t.pencolor("cyan")
t.fd(40)
elif i == '+':
if DrawMode==0:
t.write('日',font=("Arial", 18, "normal"))
t.colormode(255)
#t.pencolor(69,137,148)
t.goto(-200,-100)
elif i == ':':
if DrawMode==0:
t.rt(90)
t.fd(40)
t.write(':',font=("Arial", 50, "normal"))
t.bk(40)
t.lt(90)
t.fd(40)
else:
drawDigit(eval(i))
def bgpic(self, picname=None):
if picname is None:
return self._bgpicname
if picname not in self._bgpics:
self._bgpics[picname] = self._image(picname)
self._setbgpic(self._bgpic, self._bgpics[picname])
self._bgpicname = picname
def Main():
global DrawMode
sta=time.perf_counter()
t.setup(1000,750)
t.clear()
t.reset()
t.bgpic(r'./1.gif')
t.speed(0)
t.tracer(False)
t.Turtle().screen.delay(0)
#while True:
t.penup()
t.goto(-450,100)
t.pensize(3)
t.pencolor("gray")
DrawMode=1
timeTemp=time.localtime()
drawDate(time.strftime('%Y-%m=%d+%H:%M:%S',timeTemp))
DrawMode=0
t.pensize(5)
t.pencolor("cyan")
t.penup()
t.goto(-450,95)
drawDate(time.strftime('%Y-%m=%d+%H:%M:%S',timeTemp))
t.hideturtle()
end=time.perf_counter()
t.ontimer(Main,int(1000-1000*end+1000*sta)) #此函数用于安装计时器,该计时器在int毫秒后调用Main
t.done()
#t.mainloop()
Main()
1.首先把当前时间转换为毫秒;获取毫秒值;
2.1秒钟调用一次,然后减一秒;
3.转换为日期格式显示。