绘制数码管
python代码
import turtle as tl
import time
def drawGap(): # 绘制数码管间隔
tl.pu()
tl.fd(9)
def drawLine(draw): # 绘制单段数码管
drawGap()
tl.pendown() if draw else tl.penup()
tl.fd(40)
drawGap()
tl.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)
tl.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)
tl.left(180)
tl.penup()
tl.fd(20)
def drawDate(date):
tl.pencolor("#AB82FF")
for i in date:
if i == '-':
tl.write('年', font=("Arial", 22, "normal"))
tl.pencolor("#B3EE3A")
tl.fd(40)
elif i == '=':
tl.write('月', font=("Arial", 22, "normal"))
tl.pencolor("#FFD700")
tl.fd(40)
elif i == '+':
tl.write('日', font=("Arial", 22, "normal"))
else:
drawDigit(eval(i))
def count_days(start_date, end_date):
start_time = time.strptime(start_date, "%Y-%m-%d")
end_time = time.strptime(end_date, "%Y-%m-%d")
start_timestamp = time.mktime(start_time)
end_timestamp = time.mktime(end_time)
total_days = int((end_timestamp - start_timestamp) / (24 * 3600))
return total_days
def format_time_difference(days):
years = days // 365
months = (days % 365) // 30
remaining_days = (days % 365) % 30
return years, months, remaining_days
def all_days(start_date, end_date):
total_days = count_days(start_date, end_date)
drawDate(str(total_days))
tl.write('天', font=("Arial", 40, "normal"))
def turtle_date():
tl.speed(0)
tl.color('MediumTurquoise')
tl.penup()
tl.goto(-350, 360)
tl.pendown()
tl.write('今天是:', font=("Arial", 22, "normal"))
tl.pensize(5)
tl.penup()
tl.goto(-350, 300)
tl.pendown()
drawDate(time.strftime('%Y-%m=%d+', time.gmtime()))
tl.color('MediumTurquoise')
tl.penup()
tl.goto(-350, 190)
tl.pensize(1)
tl.pendown()
tl.pencolor("MediumTurquoise")
tl.write('我加入 Python 团队的时间是:', font=("Arial", 22, "normal"))
tl.penup()
tl.goto(-350, 110)
tl.pendown()
tl.pensize(5)
drawDate('2019-09=03+')
# 计算当前时间和加入团队的时间差
current_date = time.strftime('%Y-%m-%d', time.gmtime())
join_date = '2019-09-03'
time_difference = count_days(join_date, current_date)
years, months, days = format_time_difference(time_difference)
# 用数码管表示当前时间和加入团队的时间差
tl.penup()
tl.goto(-350, 0)
tl.pensize(1)
tl.pendown()
tl.pencolor("MediumTurquoise")
tl.write('我和团队成员一起度过了:', font=("Arial", 22, "normal"))
tl.penup()
tl.goto(-350, -70)
tl.pensize(5)
tl.pendown()
drawDate(f'{years}-{months}={days}+')
tl.penup()
tl.goto(-350, -250)
tl.pensize(1)
tl.pendown()
tl.pencolor("SlateBlue")
tl.write('总共', font=("Arial", 40, "normal"))
tl.penup()
tl.goto(-300, -250)
tl.pensize(5)
tl.pendown()
tl.pencolor("SlateBlue")
all_days('2019-09-03', time.strftime('%Y-%m-%d', time.gmtime()))
tl.done()
turtle_date()
运行结果: