python学习入门,用七段数码管显示日期
在线制作工具
#用七段数码管显示日期
import turtle as t
def drawLine(draw): #绘制单个数码管
t.pendown() if draw else
t.penup()
t.fd(50)
t.rt(90)
def drawDigit(digit): #根据数字绘制七段数码管
drawLine(True) if digit
in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,6,8] else drawLine(False)
t.left(90)
drawLine(True) if digit
in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,3,5,6,7] else drawLine(False)
drawLine(True) if digit
in [0,1,2,3,4,7,8,9] else drawLine(False)
t.lt(180)
t.penup()
t.fd(20)
def drawDate(date): #通过eval()将数字转整数
for i in date:
drawDigit(eval(i))
def main():
t.setup(800,350,200,200)
t.penup()
t.fd(-300)
t.pensize(5)
drawDate('20230515')
t.hideturtle()
t.done()
main()
运行结果如下:
修改成数码管的样子如下(红字为修改内容):
#用七段数码管显示日期
import turtle as t
def drawLine(draw): #绘制单个数码管
t.penup()
t.fd(5)
t.pendown() if draw else
t.penup()
t.fd(50)
t.penup()
t.fd(5)
t.rt(90)
def drawDigit(digit): #根据数字绘制七段数码管
drawLine(True) if digit
in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,6,8] else drawLine(False)
t.left(90)
drawLine(True) if digit
in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,3,5,6,7] else drawLine(False)
drawLine(True) if digit
in [0,1,2,3,4,7,8,9] else drawLine(False)
t.lt(180)
t.penup()
t.fd(20)
def drawDate(date): #通过eval()将数字转整数
for i in date:
drawDigit(eval(i))
def main():
t.setup(800,350,200,200)
t.penup()
t.fd(-300)
t.pensize(5)
drawDate('20230515')
t.hideturtle()
t.done()
main()
修改成彩色数码管,显示的是当前日期,修改如下(红字为修改内容):
#用七段数码管显示日期
import turtle as t
import random
import datetime
t.colormode(255)
t.speed("fastest")
def drawLine(draw): #绘制单个数码管
t.penup()
t.fd(5)
t.pendown() if draw else
t.penup()
t.fd(50)
t.penup()
t.fd(5)
t.rt(90)
def drawDigit(digit): #根据数字绘制七段数码管
drawLine(True) if digit
in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,6,8] else drawLine(False)
t.left(90)
drawLine(True) if digit
in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit
in [0,2,3,5,6,7] else drawLine(False)
drawLine(True) if digit
in [0,1,2,3,4,7,8,9] else drawLine(False)
t.lt(180)
t.penup()
t.fd(20)
def drawDate(date): #通过eval()将数字转整数
for i in date:
r=random.randint(0,255)
g=random.randint(0,255)
b=random.randint(0,255)
t.pencolor(r,g,b)
drawDigit(eval(i))
def main():
#t.setup(800,350,200,200)
t.penup()
t.fd(-300)
t.pensize(5)
current_day=datetime.date.today()
today_string = current_day.strftime('%Y%m%d')
drawDate(today_string)
t.hideturtle()
t.done()
main()
- 0003
- 0000
- 0002
- 0000
- 0000