ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python小实例——七段数码管绘制

2020-01-30 13:06:11  阅读:487  来源: 互联网

标签:digit False drawLine python else 数码管 fd True 七段


首先用time库获取系统当前时间

然后用turtle库画出来

算是对于turtle库内函数的一次实践运用叭

import turtle as t
import time

def drawGap()://因为是数码管所以线条要有空隙
    t.penup()
    t.fd(5)

def drawLine(draw)://画一条线
    drawGap()
    t.pendown() if draw else t.penup()//画不画取决于参数为True还是为False
    t.fd(50)
    drawGap()
    t.right(90)

def drawDigit(digit)://绘制数字,不管数字是几都要调用七次画线函数,只不过真假值不同
    drawLine(True) if digit in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
    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(25)


def drawDate(date):
    t.pencolor('red')

    for i in date:
        if i == '=':
            t.write("年", font=("Arial", 25, "normal"))
            t.pencolor('green')//画笔颜色切换
            t.fd(50)
        elif i == '+':
            t.write("月", font=("Arial", 25, "normal"))
            t.pencolor('blue')
            t.fd(50)
        elif i == '-':
            t.write("日", font=("Arial", 25, "normal"))
            t.fd(50)
        else:
            drawDigit(eval(i))

def main():
    t.setup(1000, 800, 200, 200)
    t.speed(10)
    t.penup()
    t.fd(-400)//刚开始海龟在屏幕正中间,先让往左靠靠
    t.pensize(5)
    drawDate(time.strftime("%Y=%m+%d-", time.gmtime()))//按照自己定义的格式调用系统日期
    t.hideturtle()
    t.done()

main()//最后别忘了调用

效果:

喜欢_月夜 发布了102 篇原创文章 · 获赞 21 · 访问量 1万+ 私信 关注

标签:digit,False,drawLine,python,else,数码管,fd,True,七段
来源: https://blog.csdn.net/weixin_43721423/article/details/104114144

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有