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

作者:一只小菜ji 时间:2021-12-22 20:04:01 

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

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

这是看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实现七段数码管和倒计时效果

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

来源:https://blog.csdn.net/qq_40925165/article/details/84350112

标签:python,七段,数码管,倒计时
0
投稿

猜你喜欢

  • 在ASP.NET 2.0中操作数据之四十:自定义DataList编辑界面

    2023-07-07 04:45:20
  • python反反爬虫技术限制连续请求时间处理

    2023-08-27 13:53:04
  • 详解使用python爬取抖音app视频(appium可以操控手机)

    2023-09-20 13:30:32
  • Oracle9i取得建表和索引的DDL语句

    2010-07-20 12:59:00
  • 双击编辑功能如何实现

    2008-02-26 16:17:00
  • python开发中range()函数用法实例分析

    2021-05-08 12:53:26
  • python数据分析之员工个人信息可视化

    2023-08-05 02:32:26
  • javascript preload&lazy load

    2023-09-01 06:15:43
  • Python利器openpyxl之操作excel表格

    2022-11-16 18:46:58
  • Python 下载及安装详细步骤

    2021-05-17 05:24:24
  • Python基础教程之异常详解

    2022-02-08 05:48:49
  • python之np.argmax()及对axis=0或者1的理解

    2021-01-27 19:28:32
  • Keras中的多分类损失函数用法categorical_crossentropy

    2023-06-23 12:25:37
  • css布局查看器

    2008-10-29 11:22:00
  • Dreamweaver制作网页实用七小招

    2009-05-29 18:36:00
  • ASP万用分页程序

    2007-09-21 12:45:00
  • python之如何查找多层嵌套字典的值

    2021-12-05 08:57:07
  • Python使用pickle进行序列化和反序列化的示例代码

    2022-11-17 10:46:22
  • 在Asp程序中取得表单所有内容的方法

    2010-04-24 16:07:00
  • PyTorch中apex安装方式和避免踩坑

    2023-06-20 15:19:57
  • asp之家 网络编程 m.aspxhome.com