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

首先用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()//最后别忘了调用

效果:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值