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("black") turtle.fd(20) elif i == '月': turtle.write('月', font=("Arial", 18, "normal")) turtle.pencolor("blue") turtle.fd(20) elif i == '日': turtle.write('日', font=("Arial", 18, "normal")) turtle.pencolor("yellow") turtle.fd(20) elif 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) else: draw_digit(int(i)) def again(): turtle.speed(0) turtle.penup() turtle.fd(-300) turtle.pensize(5) current_time = datetime.datetime.now().strftime('%Y年%m月%d日%H时%M分') draw_date(current_time) time.sleep(2) turtle.reset() turtle.hideturtle() def main(): turtle.setup(800, 350, 200, 200) again() turtle.done() main()
运行结果
标签:turtle,显示,False,draw,else,数码管,fd,line,七段 From: https://www.cnblogs.com/siwu0220/p/17845168.html