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

yipeiwu_com5年前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的Tornado框架的异步任务与AsyncHTTPClient

高性能服务器Tornado Python的web框架名目繁多,各有千秋。正如光荣属于希腊,伟大属于罗马。Python的优雅结合WSGI的设计,让web框架接口实现千秋一统。WSGI 把应...

python SSH模块登录,远程机执行shell命令实例解析

用python SSH模块登录,并在远程机执行shell命令 (在CentOS 7 环境试验成功, Redhat 系列应该是兼容的。) 先安装必须的模块 # yum install...

python统计文本字符串里单词出现频率的方法

本文实例讲述了python统计文本字符串里单词出现频率的方法。分享给大家供大家参考。具体实现方法如下: # word frequency in a text # tested wit...

Python正则获取、过滤或者替换HTML标签的方法

本文实例介绍了Python通过正则表达式获取,去除(过滤)或者替换HTML标签的几种方法,具体内容如下 python正则表达式关键内容: python正则表达式转义符: . 匹配除...

python判断一个对象是否可迭代的例子

如何判断一个对象是可迭代对象? 方法是通过collections模块的Iterable类型判断: >>> from collections import Iter...