Skip to content

Latest commit

 

History

History
150 lines (124 loc) · 5.96 KB

File metadata and controls

150 lines (124 loc) · 5.96 KB

六十四、七段显示模块

原文:http://inventwithpython.com/bigbookpython/project64.html

七段显示器是一种 LCD 组件,用于在袖珍计算器、微波炉和其他小型电子设备中显示数字。通过 LCD 中七个线段的不同组合,七段显示器可以表示数字 0 到 9。它们看起来像这样:

   __         __    __          __    __   __    __    __
  |  |    |   __|   __|  |__|  |__   |__     |  |__|  |__|
  |__|    |  |__    __|     |   __|  |__|    |  |__|   __|

这个程序的好处是其他程序可以把它作为一个模块导入。项目 14,“倒计时”和项目 19,“数字钟”,导入sevseg.py文件,这样他们就可以使用它的getSevSegStr()函数。你可以在en.wikipedia.org/wiki/Seven-segment_display找到更多关于七段显示器和其他变化的信息。

运行示例

尽管它是一个模块,当你直接运行程序时,sevseg.py输出一个它产生的数字的示例演示。输出将如下所示:

This module is meant to be imported rather than run.
For example, this code:
    import sevseg
    myNumber = sevseg.getSevSegStr(42, 3)
    print(myNumber)

Will print 42, zero-padded to three digits:
 __        __
|  | |__|  __|
|__|    | |__

工作原理

getSevSegStr()函数首先创建一个包含三个字符串的列表。这些字符串表示数字的顶行、中间行和底行。第 27 行到第 75 行有一长串针对每个数字(以及小数点和减号)的if - elif语句,这些语句将每个数字的行连接到这些字符串。这三个字符串在第 84 行用换行符连接在一起,因此函数返回一个适合传递给print()的多行字符串。

"""Sevseg, by Al Sweigart email@protected
A seven-segment number display module, used by the Countdown and Digital
Clock programs.
More info at https://en.wikipedia.org/wiki/Seven-segment_display
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: short, module"""

"""A labeled seven-segment display, with each segment labeled A to G:
__A__
|     |    Each digit in a seven-segment display:
F     B     __       __   __        __   __  __   __   __
|__G__|    |  |   |  __|  __| |__| |__  |__    | |__| |__|
|     |    |__|   | |__   __|    |  __| |__|   | |__|  __|
E     C
|__D__|"""


def getSevSegStr(number, minWidth=0):
   """Return a seven-segment display string of number. The returned
   string will be padded with zeros if it is smaller than minWidth."""

   # Convert number to string in case it's an int or float:
   number = str(number).zfill(minWidth)

   rows = ['', '', '']
   for i, numeral in enumerate(number):
       if numeral == '.':  # Render the decimal point.
           rows[0] += ' '
           rows[1] += ' '
           rows[2] += '.'
           continue  # Skip the space in between digits.
       elif numeral == '-':  # Render the negative sign:
           rows[0] += '    '
           rows[1] += ' __ '
           rows[2] += '    '
       elif numeral == '0':  # Render the 0.
           rows[0] += ' __ '
           rows[1] += '|  |'
           rows[2] += '|__|'
       elif numeral == '1':  # Render the 1.
           rows[0] += '    '
           rows[1] += '   |'
           rows[2] += '   |'
       elif numeral == '2':  # Render the 2.
           rows[0] += ' __ '
           rows[1] += ' __|'
           rows[2] += '|__ '
       elif numeral == '3':  # Render the 3.
           rows[0] += ' __ '
           rows[1] += ' __|'
           rows[2] += ' __|'
       elif numeral == '4':  # Render the 4.
           rows[0] += '    '
           rows[1] += '|__|'
           rows[2] += '   |'
       elif numeral == '5':  # Render the 5.
           rows[0] += ' __ '
           rows[1] += '|__ '
           rows[2] += ' __|'
       elif numeral == '6':  # Render the 6.
           rows[0] += ' __ '
           rows[1] += '|__ '
           rows[2] += '|__|'
       elif numeral == '7':  # Render the 7.
           rows[0] += ' __ '
           rows[1] += '   |'
           rows[2] += '   |'
       elif numeral == '8':  # Render the 8.
           rows[0] += ' __ '
           rows[1] += '|__|'
           rows[2] += '|__|'
       elif numeral == '9':  # Render the 9.
           rows[0] += ' __ '
           rows[1] += '|__|'
           rows[2] += ' __|'

       # Add a space (for the space in between numerals) if this
       # isn't the last numeral and the decimal point isn't next:
       if i != len(number) - 1 and number[i + 1] != '.':
           rows[0] += ' '
           rows[1] += ' '
           rows[2] += ' '

   return '\n'.join(rows)


# If this program isn't being imported, display the numbers 00 to 99.
if __name__ == '__main__':
   print('This module is meant to be imported rather than run.')
   print('For example, this code:')
   print('    import sevseg')
   print('    myNumber = sevseg.getSevSegStr(42, 3)')
   print('    print(myNumber)')
   print()
   print('...will print 42, zero-padded to three digits:')
   print(' __        __ ')
   print('|  | |__|  __|')
   print('|__|    | |__ ') 

在输入源代码并运行几次之后,尝试对其进行实验性的修改。你也可以自己想办法做到以下几点:

  • 为数字创建新的字体,比如使用五行和chr(9608)返回的块字符串。
  • 查看维基百科关于七段显示的文章,了解如何显示字母,然后将它们添加到sevseg.py
  • en.wikipedia.org/wiki/Sixteen-segment_display学习十六段显示,并创建一个十六段显示模块来生成该样式的数字。

探索程序

试着找出下列问题的答案。尝试对代码进行一些修改,然后重新运行程序,看看这些修改有什么影响。

  1. 如果将第 80、81 和 82 行的单空格字符串改为空字符串会发生什么?
  2. 如果将第 18 行的默认参数minWidth=0改为minWidth=8,会发生什么?