Python實現七段數碼管的日期顯示

顯示要求:

i代'd碼實u現: 

#引入turtle庫
import turtle as tu
#引入time庫
import time
#定義繪製數碼管間隔函數,即每段之間要有間隔
def drawGap():
    tu.penup()
    tu.fd(10)

#定義畫線函數
def drawLine(draw):
    drawGap() #定義每段數碼管的間隔
    tu.pendown() if draw else tu.penup()   #控制畫筆起落
    tu.fd(40)
    drawGap()
    tu.right(90)  #向右轉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)  #此時回到了起點
    tu.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)
    tu.left(180)
    tu.penup()
    tu.fd(20)

#定義所畫數據的函數
def drawDate(date):           #data爲日期,格式爲'%Y-%m=%d+'
    tu.pencolor("red")
    for i in date:
        if i == "-":
            tu.write('年', font=("Arial", 18, "normal"))  #格式規範
            tu.pencolor("green")
            tu.fd(40)
        elif i == '=':
            tu.write('月', font=("Arial", 18, "normal"))
            tu.pencolor("blue")
            tu.fd(40)
        elif i == '+':
            tu.write('日', font=("Arial", 18, "normal"))
        else:
            drawDigit(eval(i))

#定義主函數調用這寫函數
def main():
    tu.setup(1000, 350, 200, 200)
    tu.penup()
    tu.fd(-300)
    tu.pensize(5)
    drawDate(time.strftime("%Y-%m=%d+", time.gmtime()))  #time中的strftime函數對時間進行格式化
    tu.hideturtle()
    tu.done()

#執行main函數
main()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章