Python colorful digital tube

First start the software, because of the mac environment, use Command+N (windows: Ctrl+N) to create a new text-based interactive window, and then Command+S (windows: Ctrl+S) to save the location

Use python to draw the current date and time

Next start editing the code

#七段数码管的绘制.py
 
from turtle import *
from random import *
import time 
 
#绘制单段间隔
def drawGap():
    penup()
    fd(5)
 
#绘制单段数码管
def drawLine(draw):
    drawGap()                                                          #调用drawGap()函数
    if draw:
        pendown()
    else:
        penup()
    fd(40)
    drawGap()
    right(90)
    
 
 
#根据数字绘制七段数码管    
def drawDigit(digit):
    pencolor(random(),random(),random())                                #随机获取0-1的颜色
    drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False)     #调用drawLine()函数
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False)
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,2,6,8] else drawLine(False)
    pencolor(random(),random(),random())
    left(90)
    drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False)
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False)
    pencolor(random(),random(),random())
    left(180)
    penup()                                                            #为绘制后续数字确定位置
    fd(20)                                                             #为前后两数字之间分离的距离
 
#获得要输出的数字
def drawData(date):
    for i in date:
        if i=='-':
            write('年',font=('幼圆',20,'normal'))
            fd(40)
        elif i=='=':
            write('日',font=('华文云彩',20,'normal'))
            fd(40)
        elif i=='+':
            write('月',font=('隶书',20,'normal'))
            fd(40)
        elif i=='$':
            write('时',font=('幼圆',20,'normal'))
            fd(40)
        elif i=='&':
            write('分',font=('华文云彩',20,'normal'))
            fd(40)
        elif i=='#':
            write('秒',font=('隶书',20,'normal'))
            fd(40)
        else:
            drawDigit(eval(i))
        
 
 
#主函数(设置画布与画笔等)
 
def main():
    setup(1350,350,0,0)
    speed(100)
    penup()
    fd(-600)
    pensize(5)
    drawData(time.strftime('%Y-%m+%d=%H$%M&%S#',time.localtime()))
    
 
#调用主函数
main()

After Fn+F5 is running, you can start drawing

 

Guess you like

Origin blog.csdn.net/m0_74436853/article/details/131032761