python turtle 绘制七段数码管以及14段数码管显示字母和时间

Posted ( ̄¬ ̄*)流口水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python turtle 绘制七段数码管以及14段数码管显示字母和时间相关的知识,希望对你有一定的参考价值。

书上的7段数码管,显示时间

书上的7断数码管显示应功都看过了,这里顺便提供一下`

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()

看看显示原理

我们把这里所有的False全部改为True,就可以明白7段是怎么绘制的了.
改完运行下,就知道了.

14段模型

参考下网上的14段图片


通过小学知识可以知道,这里我们得写14笔,完成一个14位数字管的.
画了一个顺序草图
大概就这样把,然后上代码就完事了.

14位数码管,画英文,和数字

import datetime
import turtle

name = []

def drawLine(draw):  # 绘制单段数码管
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    turtle.right(90)


def drawLine1(draw):  # 绘制单段数码管1
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(20)


def drawtdown1(draw):  # 斜线
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(44.7)


def drawDigit(digit):  # 根据数字绘制七段数码管
    # 横1笔
    drawLine1(True) if digit in [2, 4, 5, 6, 8, 9, 'A', 'E', 'F', 'H',  'K', 'P', 'R', 'S'] else drawLine1(False)
   
    # 横2笔
    drawLine1(True) if digit in [2, 3, 4, 5, 6, 8, 9,'F', 'A','B','G','H','P,'R',S'] else drawLine1(False)
   
    # 第3笔
    turtle.right(90)
    drawLine(True) if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'D', 'G', 'H', 'J', 'M', 'N' , 'O', 'Q', 'S', 'U', 'W'] else drawLine(False)
   
    # 第4笔
    drawLine(True) if digit in ['L',0, 2, 3, 5, 6, 8, 9, 'B', 'C', 'D', 'E', 'G', 'I', 'U','J', 'O', 'Q', 'S', 'Z'] else drawLine(False)
    
    # 第5笔
    drawLine(True) if digit in ['L',0, 2, 6, 8, 'A', 'C' , 'E' , 'F' , 'G', 'H' , 'J' , 'K' , 'L' , 'M' , 'N' ,'O' , 'P' , 'Q' ,'R' , 'U' , 'V' , 'W'] else drawLine(False)
   
    # 第6笔(因为会右转,这里调节第五笔为向上直线)
    turtle.left(90)
    drawLine(True) if digit in ['L',0, 4, 5, 6, 8, 9, 'A','C','E','F','G','H','K','M','N','O','P','Q','R','S','U','V','W'] else drawLine(False)
  
    # 第7笔
    drawLine(True) if digit in [0, 2, 3, 5, 6, 7, 8, 9, 'A','B','C','D','E','F','G','O','P','Q','R','I','S','T','Z'] else drawLine(False)
   
    # 第8笔
    drawLine(True) if digit in [0, 1, 2, 3, 4, 7, 8, 9, 'A','B','D','H','J','M','N','O','P','Q','R','U','W'] else drawLine(False)
  
    # 第9笔
    turtle.left(90)
    turtle.penup()
    turtle.fd(40)
    turtle.right(90 + 63.4)
    drawtdown1(True) if digit in ['K','N','Q','K','N','Q','R','W','X'] else drawtdown1(False)
  
    # 第10笔********************
    drawtdown1(True) if digit in ['M','N','X','Y'] else drawtdown1(False)
  
    # 第11笔********************
    turtle.right(90 + 26.6)
    turtle.penup()
    turtle.fd(20)
    turtle.right(90)
    drawLine(True) if digit in ['B','D','I','T'] else drawLine(False)
   
    # 第12笔***********
    turtle.left(90)
    drawLine(True) if digit in ['B','D','I','T','Y'] else drawLine(False)
  
    # 第13笔
    turtle.penup()
    turtle.fd(20)
    turtle.right(90 + 26.6)
    drawtdown1(True) if digit in ['V','X','Z','W', 0] else drawtdown1(False)

    # 第14笔
    drawtdown1(True) if digit in ['M','V','X','Y','Z','K', 0, 1] else drawtdown1(False)
    turtle.right(90 + 63.4)
    turtle.penup()
    turtle.fd(40)
    turtle.left(90)
    turtle.fd(20)


def drawDate(date):
    turtle.pencolor("black")
    for i in date:
        if i == '-':
            turtle.write('年', font=("Arial", 25, "normal"))
            turtle.pencolor("black")
            turtle.fd(40)
        elif i == '=':
            turtle.write('月', font=("Arial", 25, "normal"))
            turtle.pencolor("black")
            turtle.fd(40)
        elif i == '+':
            turtle.write('日', font=("Arial", 25, "normal"))
        else:
            drawDigit(eval(i))


def drawname():
    #name = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    name = ['L', 'O', 'N', 'G',' ', 'T', 'A', 'O']
    #name = [ 'I','  ','L','O','V','E', ' ',  'Y','O','U']
    for i in name:
        drawDigit(i)


def main():
    turtle.setup(800, 350, 200, 200)
    turtle.penup()
    turtle.goto(-300, 50)
    turtle.pensize(5)
    turtle.pencolor("black")
    turtle.speed(0)#这个让乌龟跑快点,慢慢爬真的难受,想慢点的同志把这个注释就行
    drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
    turtle.penup()
    turtle.goto(-200, -100)
    turtle.pencolor('red')
    drawname()
    turtle.hideturtle()
    turtle.exitonclick()#画完不关闭画布,点击关闭


main()

运行就是这样啦,这里画了字母和数字

想画自己名字字母的,在name 里面添加名字字母的字符就行

下面又搞了个字体会变颜色的,还有背景闪烁的,嘿嘿

import datetime
import turtle
import random

name = []


def random_color():  #网上找的随机颜色
    colors1 = '0123456789ABCDEF'
    num = "#"
    for i in range(6):
        num += random.choice(colors1)
    return num


def drawLine(draw):  # 绘制单段数码管

    turtle.pencolor(random_color())
    turtle.bgcolor(random_color())
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)

    turtle.right(90)


def drawLine1(draw):  # 绘制单段数码管1

    turtle.pencolor(random_color())
    turtle.bgcolor(random_color())
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(20)


def drawtdown1(draw):  # 下1

    turtle.pencolor(random_color())
    turtle.bgcolor(random_color())
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(44.7)


def drawDigit(digit):  # 根据数字绘制七段数码管

    # 横1笔
    drawLine1(True) if digit in [2, 4, 5, 6, 8, 9, 'A', 'E', 'F', 'H', 'K', 'P', 'R', 'S'] else drawLine1(False)

    # 横2笔
    drawLine1(True) if digit in [2, 3, 4, 5, 6, 8, 9, 'F', 'A', 'B', 'G', 'H', 'P,'R',S'] else drawLine1(False)

    # 第3笔
    turtle.right(基于turtle库的七段数码管绘制

Python七段数码显示管

七段数码管

数码七段管时间表

Python绘制数码管显示当前时间

python(10)---七段数码管