Python繪制七段數碼管實例代碼
更新時間:2017年12月20日 14:36:18 作者:寧生信
這篇文章主要介紹了Python繪制七段數碼管實例代碼,具有一定借鑒價值,需要的朋友可以參考下。
七段數碼管(seven-segmentindicator)由7段數碼管拼接而成,每段有亮或不亮兩種情況,改進型的七段數碼管還包括一個小數點位置
繪制模式:
input:輸入當前日期的數字形式
process:根據每個數字繪制七段數碼管表示
output:繪制當前日期的七段數碼管表示
示例一:
#DrawSevenSegDisplay.py import turtle, datetime def drawLine(draw): #繪制單段數碼管 turtle.pendown() if draw else turtle.penup() turtle.fd(40) 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): #獲得要輸出的數字 for i in date: drawDigit(eval(i)) #注意: 通過eval()函數將數字變?yōu)檎麛? def main(): turtle.setup(800, 350, 200, 200) turtle.penup() turtle.fd(-300) turtle.pensize(5) drawDate(datetime.datetime.now().strftime('%Y%m%d')) turtle.hideturtle() main()
效果展示:
示例二:
#DrawSevenSegDisplay.py import turtle, datetime 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(d): #根據數字繪制七段數碼管 drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False) drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False) drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False) drawLine(True) if d in [0,2,6,8] else drawLine(False) turtle.left(90) drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False) drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False) drawLine(True) if d 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(-350) turtle.pensize(5) drawDate(datetime.datetime.now().strftime('%Y-%m=%d+')) turtle.hideturtle() main()
效果展示:
總結
以上就是本文關于Python繪制七段數碼管實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
Python如何快速生成本項目的requeirments.txt實現
本文主要介紹了Python如何快速生成本項目的requeirments.txt實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03Python中使用logging模塊代替print(logging簡明指南)
這篇文章主要介紹了Python中使用logging模塊代替print的好處說明,主旨是logging模塊簡明指南,logging模塊的使用方法介紹,需要的朋友可以參考下2014-07-07Python cookbook(數據結構與算法)從序列中移除重復項且保持元素間順序不變的方法
這篇文章主要介紹了Python cookbook(數據結構與算法)從序列中移除重復項且保持元素間順序不變的方法,涉及Python針對列表與字典的元素遍歷、判斷、去重、排序等相關操作技巧,需要的朋友可以參考下2018-03-03