版本一(通过输入数字绘制七段数码管):
1 import turtle 2 def drawLine(draw): 3 turtle.pendown() if draw else turtle.penup() 4 turtle.fd(40) 5 turtle.right(90) 6 def drawDigit(digit): 7 drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False) 8 drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False) 9 drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False) 10 drawLine(True) if digit in [0,2,6,8] else drawLine(False) 11 turtle.left(90) 12 drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False) 13 drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False) 14 drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False) 15 turtle.left(180) 16 turtle.penup() 17 turtle.fd(20) 18 def drawData(date): 19 for i in date: 20 drawDigit(eval(i)) 21 def main(): 22 turtle.setup(800,350,200,200) 23 turtle.penup() 24 turtle.fd(-300) 25 turtle.pensize(5) 26 drawData('20230205') 27 turtle.hideturtle() 28 turtle.done() 29 main()
版本二(通过获取系统时间绘制七段数码管):
1 import turtle,time 2 def drawGap(): 3 turtle.penup() 4 turtle.fd(5) 5 def drawLine(draw): 6 drawGap() 7 turtle.pendown() if draw else turtle.penup() 8 turtle.fd(40) 9 drawGap() 10 turtle.right(90) 11 def drawDigit(digit): 12 drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False) 13 drawLine(True) if digit in [1,3,4,5,6,7,8,9,0] else drawLine(False) 14 drawLine(True) if digit in [2,3,5,6,8,9,0] else drawLine(False) 15 drawLine(True) if digit in [2,6,8,0] else drawLine(False) 16 turtle.left(90) 17 drawLine(True) if digit in [4,5,6,7,8,9,0] else drawLine(False) 18 drawLine(True) if digit in [2,3,5,6,7,8,9,0] else drawLine(False) 19 drawLine(True) if digit in [1,2,3,4,7,8,9,0] else drawLine(False) 20 turtle.left(180) 21 turtle.penup() 22 turtle.fd(20) 23 def drawData(date): 24 turtle.pencolor('red') 25 for i in date: 26 if i == '-': 27 turtle.write('年',font = ('Arial',18,'normal')) 28 turtle.pencolor('green') 29 turtle.fd(40) 30 elif i == '=': 31 turtle.write('月',font = ('Arial',18,'normal')) 32 turtle.pencolor('blue') 33 turtle.fd(40) 34 elif i == '+': 35 turtle.write('日',font = ('Arial',18,'normal')) 36 else: 37 drawDigit(eval(i)) 38 def main(): 39 turtle.setup(800,350,200,200) 40 turtle.penup() 41 turtle.fd(-400) 42 turtle.pensize(5) 43 drawData(time.strftime('%Y-%m=%d+',time.gmtime())) 44 turtle.hideturtle() 45 turtle.done() 46 main()
标签:turtle,digit,False,drawLine,True,else,数码管,绘制,七段 From: https://www.cnblogs.com/T-2187803607/p/17094723.html