运用数码管打印当前日期和时间
#七段电子数码管绘制
#导入库模块
from turtle import *
from random import *
#绘制单段间隔
def drawGap():
penup()
fd(5)
#绘制单段数码管
def drawLine(draw):
drawGap()
if draw:
pendown()#画笔设置为工作模式
else:
penup()
fd(20)#画笔前进20
drawGap()
right(90)#向右旋转90度
#根据数字绘制七段数码管
def drawDigit(digit):
#第一段
pencolor(random(),random(),random())#借用随机库
drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False)
#第二段
pencolor(random(),random(),random())#借用随机库
drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False)
#第三段
pencolor(random(),random(),random())#借用随机库
drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)
#第四段
pencolor(random(),random(),random())#借用随机库
drawLine(True) if digit in [0,2,6,8] else drawLine(False)
left(90)
#第五段
pencolor(random(),random(),random())#借用随机库
drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)
#第六段
pencolor(random(),random(),random())#借用随机库
drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False)
#第七段
pencolor(random(),random(),random())#借用随机库
drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False)
left(180)
penup()
fd(20)
#获得想要的数字
def drawDate(date):
for i in date:
drawDight(eval(i))
#主函数
def main():
setup(800,350,200,20)
penup()
fd(-1500)
pensize(5)
drawDate("20231031")
hideturtle
done()
#调用函数
import datetime
current_date = datetime.datetime.now().date()
print('当前日期:',current_date)