欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

turtle实例5 数码管

程序员文章站 2022-07-15 09:55:59
...

数码管规格

百科

led数码管(LED Segment Displays)由多个发光二极管封装在一起组成“8”字型的器件,引线已在内部连接完成,只需引出它们的各个笔划,公共电极。数码管实际上是由七个发光管组成8字形构成的,加上小数点就是8个。这些段分别由字母a,b,c,d,e,f,g,dp来表示。没有小数点的叫七段数码管

一个八段数码管称为一位,多个数码管并列在一起可构成多位数码管,它们的段选线(即a,b,c,d,e,f,g,dp)连在一起,而各自的公共端称为位选线。显示时,都从段选线送入字符编码,而选中哪个位选线,哪个数码管便会被点亮。

数码管编码

turtle实例5 数码管

绘制思路
  1. 通过控制turtle的落笔和抬笔来模拟数码管的亮与不亮
  2. 通过控制画笔颜色来模拟数码管的亮与不亮.
    俩种方法都很简单, 就不具体介绍了

运行结果

三个图分别为: “全显示”, “本地日期”, “十六进制整数”
turtle实例5 数码管turtle实例5 数码管turtle实例5 数码管
好了, 上代码,

完整代码

# 型号:3361BS

import turtle, time, random
tubeDict={
    '0': '1111110',
    '1': '0110000',
    '2': '1101101',
    '3': '1111001',
    '4': '0110011',
    '5': '1011011',
    '6': '1011111',
    '7': '1110000',
    '8': '1111111',
    '9': '1111011',
    'A': '1110111',
    'b': '0011111',
    'c': '1001110',
    'd': '0111101',
    'E': '1001111',
    'F': '1000111',
    '.': '.'
}

turtle.setup(startx=0)
tl=80
width = tl*0.8/9.2
spacing=2*(tl-width)*7.5/9.2
y0 = tl
def rgbToHex(r,g,b):
    return '#{:0>2}{:0>2}{:0>2}'.format(hex(r%256)[2:], hex(g%256)[2:], hex(b%256)[2:])

def drawTube(string='1111111', x0=0, rangle = 5, clear=False, sleep = 0, tl=tl):
    # 9.2:7.5:4.8:0.8:0.2, 5°.
    color2 = rgbToHex(10,10,10)
    tube = turtle.Pen()
    tube.ht(), tube.color('red'), tube.speed(0)
    tube.width(width)

    # draw dot. add in 2019/11/11
    tube.pu(), tube.goto(x0, y0), tube.seth(0), tube.right(90 + rangle), tube.fd(tl * 2 - width), tube.left(
        90 + rangle), tube.fd(spacing * 0.8), tube.pd()
    if string == '.':
        tube.color('red'), tube.dot(width)
        return
    
    tube.pu(), tube.goto(x0,y0), tube.seth(0)
    tube.pu(), tube.fd(width*0.5), tube.pd()
    if string[0] == '0':
        tube.color(color2)
    else:
        tube.color('red')
    tube.fd(tl-width)
    tube.pu(), tube.fd(width*0.5), tube.pd()
    tube.right(90+rangle)
    
    tube.pu(), tube.fd(width), tube.pd()
    if string[1] == '0':
        tube.color(color2)
    else:
        tube.color('red')
    tube.fd(tl-width*2)
    tube.pu(), tube.fd(width), tube.pd()
    
    if string[2] == '0':
        tube.color(color2)
    else:
        tube.color('red')
    tube.fd(tl - width*2)
    tube.pu(), tube.fd(width), tube.pd()
    tube.right(90-rangle)
    tube.pu(), tube.fd(width * 0.5), tube.pd()
    
    if string[3] == '0':
        tube.color(color2)
    else:
        tube.color('red')
    tube.fd(tl-width)
    tube.pu(), tube.fd(width * 0.5), tube.pd()
    tube.right(90+rangle)
    tube.pu(), tube.fd(width), tube.pd()
    
    if string[4] == '0':
        tube.color(color2)
    else:
        tube.color('red')
    tube.fd(tl - width * 2)
    tube.pu(), tube.fd(width), tube.pd()
    
    if string[5] == '0':
        tube.color(color2)
    else:
        tube.color('red')
    tube.fd(tl - width * 2)
    tube.pu(), tube.fd(width), tube.pd()
    
    tube.pu(), tube.goto(x0, y0), tube.seth(0), tube.right(90+rangle), tube.fd(tl-width*0.5), tube.left(90+rangle), tube.fd(width*0.5), tube.pd()
    
    if string[6] == '0':
        tube.color(color2)
    else:
        tube.color('red')
    tube.fd(tl-width)
    
    
    # print(string)
    time.sleep(sleep)
    if clear == True:
        tube.clear()
def drawCheck():
    for string in tubeDict:
        print(string, tubeDict[string])
        drawTube(tubeDict[string],clear=True,sleep=0.5)
        # input('enter to continue')
    strings = '8.'*8
    les = len(strings) - strings.count('.')
    turtle.tracer(False)
    for n, string in enumerate(strings):
        if string != '.':
            x0 = -spacing * (les / 2 - n/2)
        drawTube(tubeDict[string], x0=x0)
    turtle.update()
    time.sleep(1)
def displayDate():
    date = '{:0>2}{:0>2}{:0>2}'.format(time.localtime()[0], time.localtime()[1], time.localtime()[2])
    print(date)
    for n, string in enumerate(date):
        x0 = -spacing * (4 - n)
        drawTube(tubeDict[string], x0=x0)
    print(string)
def drawRandom():
    turtle.clear()
    while True:
        strings = ''
        for i in range(8):
            randint = random.choice('0123456789AbcdEF')  # hex
            strings += str(randint)
        decimal = eval('0x'+strings)
        turtle.tracer(False)
        for n, string in enumerate(strings):
            x0 = -spacing * (len(strings)/2 - n)
            drawTube(tubeDict[string], x0=x0)
        turtle.update()
        print('displaying:\n', strings, decimal)
        time.sleep(2)
        
def main():
    turtle.bgcolor('black'), turtle.setup(spacing * 10)
    drawCheck() # no error, 2019/11/10
    # displayDate()
    # time.sleep(2)
    # drawRandom()
    turtle.done()
main()