import turtle
import datetime
import time
def draw_gap(): # 绘制数码间隔
turtle.penup()
turtle.fd(5)
def draw_line(draw): # 绘制单段数码管
draw_gap()
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
draw_gap()
turtle.right(90)
def draw_digit(d): # 根据数字绘制七段数码管
draw_line(True) if d in [2, 3, 4, 5, 6, 8, 9] else draw_line(False)
draw_line(True) if d in [0, 1, 3, 4, 5, 6, 7, 8, 9] else draw_line(False)
draw_line(True) if d in [0, 2, 3, 5, 6, 8, 9] else draw_line(False)
draw_line(True) if d in [0, 2, 6, 8] else draw_line(False)
turtle.left(90)
draw_line(True) if d in [0, 4, 5, 6, 8, 9] else draw_line(False)
draw_line(True) if d in [0, 2, 3, 5, 6, 7, 8, 9] else draw_line(False)
draw_line(True) if d in [0, 1, 2, 3, 4, 7, 8, 9] else draw_line(False)
turtle.left(180)
turtle.penup()
turtle.fd(20)
def draw_date(date):
turtle.pencolor("blue")
for i in date:
if i == '-':
turtle.write('时', font=("Arial", 18, "normal"))
turtle.pencolor("green")
turtle.fd(40)
elif i == '=':
turtle.write('分', font=("Arial", 18, "normal"))
turtle.pencolor("red")
turtle.fd(40)
elif i == '+':
turtle.write('秒', font=("Arial", 18, "normal"))
turtle.pencolor("skyblue")
turtle.fd(40)
elif i == '$':
turtle.write('星期', font=("Arial", 18, "normal"))
turtle.fd(60)
else:
draw_digit(eval(i))
def again(s):
turtle.speed(0)
turtle.penup()
turtle.fd(-300)
turtle.pensize(5)
draw_date(datetime.datetime.now().strftime('%H-%M=%S+$%w'))
time.sleep(0.5)
turtle.reset()
turtle.hideturtle()
def main():
turtle.setup(800, 350, 200, 200)
again(6)
main()
标签:turtle,draw,False,else,数码管,fd,line,绘制 From: https://www.cnblogs.com/Jan25-BH/p/17845592.html