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

yipeiwu_com6年前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设计】。

相关文章

Python清空文件并替换内容的实例

有个文本文件,需要替换里面的一个词,用python来完成,我是这样写的: def modify_text(): with open('test.txt', "r+") as f:...

Python生成pdf文件的方法

本文实例演示了Python生成pdf文件的方法,是比较实用的功能,主要包含2个文件。具体实现方法如下: pdf.py文件如下: #!/usr/bin/python from repo...

Python将图片转换为字符画的方法

Python将图片转换为字符画的方法

最近在学习Python,看到网上用Python将图片转换成字符画便来学习一下 题目意思是,程序读入一个图片,以txt格式输出图片对应的字符画,如图所示: 以下是Python代码:...

python中私有函数调用方法解密

本文实例讲述了python中私有函数调用方法。分享给大家供大家参考,具体如下: 与大多数语言一样,Python 也有私有的概念: ① 私有函数不可以从它们的模块外面被调用 ② 私有类方法...

Django的分页器实例(paginator)

先导入模块: from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 分页器paginat...