Python海龟库绘制七段数码管显示当前系统时间&倒计时

今天是学习Python的第四天,写代码的时候遇到一些问题,突然想到可以把代码保存下来,将来有一天可以回过头来看看自己的成长,也许能起到督促的作用。万一跟我学习内容一样的朋友遇到问题也能做个参考。

显示当前系统时间

import turtle,time
def drawGap():#数码管之间留白
    turtle.penup()
    turtle.fd(5)
def drawLine(draw):#画一条线
    drawGap()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    drawGap()
    turtle.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)
    turtle.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)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)
def drawDate(date):#日期处理
    turtle.pencolor("red")
    for i in date:
        if i=="-":
            turtle.write("年",font=("Arial",18,"normal"))
            turtle.pencolor("green")
            turtle.fd(40)
        elif i=="=":
            turtle.write("月",font=("Arial",18,"normal"))
            turtle.pencolor("blue")
            turtle.fd(40)
        elif i=="+":
            turtle.write("日",font=("Arial",18,"normal"))
        else:
            drawDigit(eval(i))
def main():
    turtle.setup(800,350,200,200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(time.strftime("%Y-%m=%d+",time.gmtime()))
    #drawDate('20181010')
    turtle.hideturtle()
    turtle.done()
main()

倒计时功能

能力有限,暂时只能写9-0的倒计时

import turtle
def drawGap():
    turtle.penup()
    turtle.fd(5)
def drawLine(draw):
    drawGap()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    drawGap()
    turtle.right(90)
def drawDigits(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)
    turtle.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)
    turtle.left(180)
    turtle.penup()
    turtle.fd(-40)#后退
    turtle.clear()#清空重新画
def main():
    turtle.setup(300,300,200,200)
    turtle.pensize(10)
    turtle.pencolor("purple")
    for i in range(10):
        drawDigits(9-i)
    '''
    for i in range(9,-1,-1):
        drawDigits(i)
    '''
main()

你可能感兴趣的:(Python基础)