python实现七段数码管和倒计时效果

yipeiwu_com7年前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()

看到很多优秀的人,他们的努力,成就,天赋和幸运,都是我所不能及的,但若心向往,每天再努力一点点,即使最后没有向他们那样,也会使我不那么平庸。加油!!!

以上这篇python实现七段数码管和倒计时效果就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

TensorFLow用Saver保存和恢复变量

本文为大家分享了TensorFLow用Saver保存和恢复变量的具体代码,供大家参考,具体内容如下 建立文件tensor_save.py, 保存变量v1,v2的tensor到checkp...

使用C#配合ArcGIS Engine进行地理信息系统开发

使用C#配合ArcGIS Engine进行地理信息系统开发

简单的地图读取、展示 终于到暑假了。。。开始认真整理整理相关学习的心得体会咯~ 先把很久之前挖的关于C# 二次开发的坑给填上好了~ 这次先计划用一个月把C# ArcEngine 10.0...

浅谈Python peewee 使用经验

本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作。所以,没有使用过 peewee,可以先阅读文档 正确性和覆盖面...

使用 Python 获取 Linux 系统信息的代码

哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份...

python中异常捕获方法详解

在Python中处理异常使用的是try-except代码块,try-except代码块放入让python执行的操作,同时告诉python程序如果发生了异常该怎么办,try-except这...