【Python】七段数码管绘制日期年月日

七段数码管是一种展示数字的有效方式。
请用程序绘制当前系统时间对应的七段数码管,效果如下: ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪在这里插入图片描述
要求如下:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬

(1) 使用 time 库获得系统当前时间,格式如下:2020-02=14‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬+
(2) 绘制对应的七段数码管‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬,格式如下:2020年02月14日
(3) 数码管风格不限

代码如下:

import turtle as t
import time
def drawGap(): #绘制数码管间隔
    t.penup()
    t.fd(5)
def drawLine(draw): #绘制单段数码管
	drawGap()
	t.pendown() if draw else t.penup()
	t.fd(40)
	drawGap()
	t.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)
    t.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)
    t.left(180)
    t.penup()
    t.fd(20)
def drawDate(date): #date为日期,格式为'%Y-%m=%d+'
    t.pencolor("red")
    for i in date:
    	if i == '-':
    		t.write('年',font=("Arial",18,"normal"))
    		t.pencolor("green")
    		t.fd(40)
    	elif i == '=':
    		t.write('月',font=("Arial",18,"normal"))
    		t.pencolor("blue")
    		t.fd(40)
    	elif i == '+':
    		t.write('日',font=("Arial",18,"normal"))
    	else:
            drawDigit(eval(i)) #通过eval()函数将数字变为整数
def main():
    t.setup(800, 350, 200, 200)
    t.penup()
    t.fd(-300)
    t.pensize(5)
    drawDate(time.strftime('%Y-%m=%d+',time.gmtime()))
    t.done()
main()
发布了20 篇原创文章 · 获赞 38 · 访问量 899

猜你喜欢

转载自blog.csdn.net/weixin_45506775/article/details/104298620
今日推荐