python七段数码管和倒计时

8是典型的七段数码管的例子,因为刚好七段都有经过,这里我写的代码是从1开始右转。
在这里插入图片描述

这是看Mooc视频写的一个关于用七段数码管显示当前时间

# -*-coding:utf-8 -*-
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)
    t.right(90)
def drawDigit(digit):  
    drawLine(True) if digit in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)  #当digit是2, 3, 4, 5, 6, 8, 9时执行
    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, 8, 9] else drawLine(False)
    drawLine(True) if digit in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
    t.left(180)
    t.penup()
    t.fd(20)
def drawDate(date):
    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("green")
            t.fd(40)
        elif i=='+':
            t.write('日', font=("Arial", 18, "normal"))
            t.pencolor("green")
            t.fd(40)
        else:
            drawDigit(eval(i))
    # drawDigit(eval(date))
if __name__ == '__main__':
    t.setup(800,350,200,200)
    t.penup()
    t.fd(-300)
    t.pensize(5)
    drawDate(time.strftime('%Y-%m=%d+',time.gmtime())) #strftime() 函数接收以时间元组,并返回以可读字符串表示的当地时间
    # drawDate('6')
    t.hideturtle()
    t.done()

除外倒计时用七段数码管显示
在下面的代码中的datetime库对我这个新手去计算时间差来说是很方便的,另外我还学会了简写条件语句
<表达示> if <条件> else <表达示>

# -*-coding:utf-8 -*-
import turtle as t
import time
import datetime

def draw_Line(draw):
    t.pendown() if draw else t.penup() #pendown 落下画笔 penup单纯飞过去没有落笔
    t.fd(40)
    t.right(90)

def draw_Digit(digit):
    t.write('剩余时间:', font=("Arial", 18, "normal"))
    t.pencolor("green")
    t.fd(160)
    i = 0
    while i < len(digit):
        if digit[i] >= '0' and digit[i] <= '9':
            draw_Line(True) if eval(digit[i]) in [2, 3, 4, 5, 6, 8, 9] else draw_Line(False)
            draw_Line(True) if eval(digit[i]) in [1, 3,4, 5, 6,7, 8, 9, 0] else draw_Line(False)
            draw_Line(True) if eval(digit[i]) in [2, 3, 5, 6, 8, 9, 0] else draw_Line(False)
            draw_Line(True) if eval(digit[i]) in [2, 6, 8, 0] else draw_Line(False)
            t.left(90)
            draw_Line(True) if eval(digit[i]) in [4, 5, 6, 8, 9, 0] else draw_Line(False)
            draw_Line(True) if eval(digit[i]) in [2, 3, 5, 6,7, 8, 9, 0] else draw_Line(False)
            draw_Line(True) if eval(digit[i]) in [1,2, 3, 4, 7, 8, 9, 0] else draw_Line(False)
            t.left(180)
            t.penup()
            t.fd(20)
        else:
            break
        i = i + 1


if __name__ == '__main__':
    t.setup(650,350,200,200)
    t.penup()
    t.fd(-300)
    t.pensize(4)
    remain = datetime.datetime(2019, 2, 4) - datetime.datetime.now()
    s=str(remain)
    draw_Digit(s)
    t.hideturtle()
    t.done()

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值