Use the Turtle Graphics library in Python to draw a seven-segment digital tube that shows the current time Code
# coding:utf-8# Draw seven segment digital tube, display current time import Timeimport turtle as tt# draw interval def drawgap (): Tt.penup () TT.FD (5) # Draw single segment digital tube def DRA Wline (Draw): Drawgap () if (draw): Tt.pendown () Else:tt.penup () tt.fd (+) Drawgap () Tt.righ T (90) # Draw Current Time def drawdate (date): Tt.pencolor ("Red") for I in date:if i = = ' + ': Tt.write ("Year", font = ("blackbody", +, "normal")) Tt.pencolor ("green") tt.fd (elif i = = '-': Tt.write ("month ", Font= (" Black "," Tt.pencolor ") tt.fd (" Blue ") elif i = = ' * ': Tt.writ E ("Day", Font= ("Black", "Tt.pencolor") tt.fd ("purple") elif i = = ' = ': t T.write ("Time", font= ("Black", "normal") Tt.pencolor ("Yellow") tt.fd (elif i = = ' # '): Tt.write ("Min", font= ("Black", "normal")) Tt.pencolor ("Black") tt.fd (+) elif i = = ' $ ': Tt.write ("Seconds",Font= ("blackbody", +, "normal")) Else:drawdigit (eval (i)) # Draw the digital tube def drawdigit (cur_time): If cur_time in [2, 3, 4, 5, 6, 8, 9]: DrawLine (True) else:drawline (False) if cur_time in [0, 1, 3, 4, 5, 6, 7, 8, 9]: DrawLine (True) else:drawline (False) if cur_time in [0, 2, 3, 5, 6, 8, 9]: DrawLine (true) Els E:drawline (False) if cur_time in [0, 2, 6, 8]: DrawLine (True) Else:drawline (false) Tt.left (all) if cur_time in [0, 4, 5, 6, 8, 9]: DrawLine (True) else:drawline (False) if cur_time in [0, 2, 3, 5, 6, 7, 8, 9]: DrawLine (True) else:drawline (False) if cur_time in [0, 1, 2, 3, 4, 7, 8, 9]: DrawLine (True) else:drawline (False) tt.left () Tt.penup () TT.FD (20) # main function def main (): Tt.setup (16 XX, (+), Tt.penup () tt.fd ( -730) tt.pensize (5) drawdate (Time.strftime ('%y+%m-%d*%h=%m#%s$ ', Time.loca Ltime ())) Tt.hideturtLe () tt.done () if __name__ = = "__main__": Main ()
Results
Summarize
Through the MOOC network learning relevant knowledge, drawing on the relevant code, the final realization of this design.
Python draw Digital tube display current time