SoFunction
Updated on 2025-04-14

Python draws seven-segment digital tube letters

In modern electronic display technology, a seven-segment digital tube is a widely used display device, often used to display numbers, letters and some special symbols. The basic principle is to form different display contents by controlling the light-out of the seven luminous segments (a, b, c, d, e, f, g). This article will introduce in detail how to use Python to draw a seven-segment digital tube to display letters. From basic principles, design ideas to code implementation, provide complete code examples and discuss its significance in practical applications.

Basic principles of 1 and 7-segment digital tubes

A seven-segment digital tube consists of seven light emitting diode (LED) segments, which are: a, b, c, d, e, f, g. Each segment can independently control the light-out state. By combining the light-out states of different segments, numbers 0-9, some letters and some special symbols can be displayed.

There are two types of common seven-segment digital tubes: common anode and common cathode. The common termination of the common anode digital tube is connected to a high level, and it is lit by connecting each section to a low level; the common termination of the common cathode digital tube is connected to a low level, and it is lit by connecting each section to a high level.

2. Design ideas

In order to draw a seven-segment digital tube to display letters in Python, we need to complete the following steps:

  • Define the segment code of the letter: The display method of each letter on the seven-segment digital tube is determined by the combination of the light and expiration of a specific segment. We need to define its corresponding segment code for each letter.
  • Drawing the frame of seven-section digital tubes: Use the graphics library to draw the frame of a seven-segment digital tube, each segment is represented by a line segment.
  • Draw letters according to segment code: According to the segment code of the letter, light up the corresponding segment, that is, draw the corresponding line segment.
  • Show letters: Through the function interface, input letters, call the drawing function, and display the corresponding seven-segment digital tube pattern.

3. Python code implementation

To draw seven-segment digital tubes, we can use Python's graphics library, for examplematplotliborturtle. Here we usematplotlibTo achieve it.

1. Import the necessary libraries

import  as plt
import numpy as np

2. Define the segment code of the letter

The segment code of the seven-segment digital tube can be represented by a 7-bit binary number, each bit corresponds to a segment, 1 means light and 0 means shut off.

# Define the segment code of the letter, common anode method (1 in the segment code means off, 0 means on)segment_codes = {
    'A': '0111111',  # 0b0111111
    'B': '1011110',  # 0b1011110
    'C': '1100110',  # 0b1100110
    'D': '1101101',  # 0b1101101
    'E': '1111101',  # 0b1111101
    'F': '1111001',  # 0b1111001
    'G': '1101111',  # 0b1101111
    # Omit other letters and can be added as needed}
 
# Convert segment code to an array used for drawingdef code_to_segments(code):
    return [int(bit) == 0 for bit in code[::-1]]  # Invert the binary string, 0 means light, 1 means OFF

3. Draw the frame of seven-segment digital tubes

def draw_seven_segment_display(ax, segments, segment_width=0.1, segment_height=0.5, gap=0.02):
    # Set the coordinates of the seven-segment digital tube    x_centers = (-1, 1, 4)
    y_tops = [0.5] * 3 + [0] * 4
    y_bottoms = [0] * 3 + [-0.5] * 4
 
    # Draw seven segments    for i, segment in enumerate(segments):
        if segment:
            if i < 3:  # The first half                ([x_centers[i], x_centers[i + 1]], [y_tops[i], y_tops[i]], linewidth=segment_width)
                ([x_centers[i + 1], x_centers[i + 1]], [y_tops[i], y_bottoms[i + 1]], linewidth=segment_width)
            else:  # The second half                ([x_centers[i], x_centers[i - 1]], [y_bottoms[i], y_bottoms[i]], linewidth=segment_width)
                ([x_centers[i - 1], x_centers[i - 1]], [y_tops[i - 1], y_bottoms[i]], linewidth=segment_width)
        else:
            # Draw a dotted line to indicate the extinction            if i < 3:  # The first half                ([x_centers[i], x_centers[i + 1]], [y_tops[i], y_tops[i]], 'k--', linewidth=segment_width)
                ([x_centers[i + 1], x_centers[i + 1]], [y_tops[i], y_bottoms[i + 1]], 'k--', linewidth=segment_width)
            else:  # The second half                ([x_centers[i], x_centers[i - 1]], [y_bottoms[i], y_bottoms[i]], 'k--', linewidth=segment_width)
                ([x_centers[i - 1], x_centers[i - 1]], [y_tops[i - 1], y_bottoms[i]], 'k--', linewidth=segment_width)
 
    # Draw borders    ([-1, 1, 1, -1, -1], [0.5, 0.5, -0.5, -0.5, 0.5], 'k-', linewidth=segment_width)

4. Show letters

def display_letter(letter, ax):
    segments = code_to_segments(segment_codes[letter])
    draw_seven_segment_display(ax, segments)
    ax.set_aspect('equal')
    ('off')  # Close the axis 
# Test functiondef main():
    fig, ax = ()
    letter = 'A'
    display_letter(letter, ax)
    (f'Seven Segment Display: {letter}')
    ()
 
if __name__ == '__main__':
    main()

4. Code analysis

  • Segment code definitionsegment_codesThe dictionary defines the segment code corresponding to each letter. In the segment code, 1 means off and 0 means on. This is the way to common anode. If the common cathode method is used, you need to swap 0 and 1 in the segment code.
  • Segment code conversioncode_to_segmentsThe function converts the segment code into a Boolean array used to draw, with each element in the array corresponding to the light and expiration state of a segment.
  • Draw the framedraw_seven_segment_displayThe function draws the frame and segments of the seven-segment digital tube based on the incoming segment state array. The bright segment is drawn with a solid line, and the extinguishing segment is drawn with a dotted line (can be modified as needed).
  • Show lettersdisplay_letterFunctions draw functions based on letter calls and set the title of the graph.
  • Test functionmainFunction creates a drawing window, calls a function that displays letters, and displays the results.

V. Practical application and expansion

  • Dynamic display: It can be done through the animation library (e.g.) Realize the dynamic display of seven-segment digital tubes, such as displaying digital scrolling or letter transformation.
  • Hardware control: Combined with GPIO library (such as) Control the actual seven-segment digital tube hardware, deploy Python code to an embedded system, and realize dynamic display on the hardware.
  • Extended character set: It can extend segment code dictionary and support more characters and symbols, such as lowercase letters, punctuation marks, etc.
  • Graphic interface: Use the graphical interface library (e.g.tkinterPyQt) Create a user-friendly interface that allows the user to enter characters and display the corresponding seven-segment digital tube pattern.

The following is a using Python andmatplotlibA library draws a code example of a seven-segment digital tube display letters. This example will define some letter segment codes and draw the corresponding seven-segment digital tube pattern.

First, make sure you have installed itmatplotliblibrary. If not installed, you can use the following command to install:

pip install matplotlib

Then, use the following code to draw the seven-segment digital tube to display the letters:

import  as plt
import numpy as np
 
# Define the segment code of the letter (common cathode method, 0 means light, 1 means off)# The sequence of segment codes is: a, b, c, d, e, f, gsegment_codes = {
    'A': '0111111',
    'B': '1011110',
    'C': '1100110',
    'D': '1101101',
    'E': '1111101',
    'F': '1111001',
    'G': '1101111',
    # More letters can be added as needed}
 
# Draw a function of seven-segment digital tubesdef draw_seven_segment_display(ax, segments, segment_width=0.1, segment_height=0.6, gap=0.02):
    # Set the coordinates of the seven-segment digital tube    x_centers = (-1, 1, 4) + gap / 2
    y_tops = [segment_height] * 3 + [0] * 4
    y_bottoms = [0] * 3 + [-segment_height] * 4
 
    # Draw seven segments    for i, segment in enumerate(segments):
        if segment == 0:  # Duan Liang            if i < 3:  # The first half                ([x_centers[i], x_centers[i + 1]], [y_tops[i], y_tops[i]], linewidth=segment_width)
                ([x_centers[i + 1], x_centers[i + 1] - gap], [y_tops[i], y_bottoms[i + 1]], linewidth=segment_width)
            else:  # The second half                ([x_centers[i] + gap, x_centers[i - 1]], [y_bottoms[i], y_bottoms[i]], linewidth=segment_width)
                ([x_centers[i - 1], x_centers[i - 1]], [y_tops[i - 1], y_bottoms[i]], linewidth=segment_width)
        # Otherwise, the segment will be destroyed, it will not be drawn here or it can be drawn as a dotted line. 
    # Draw borders    outer_x = (([-1 - gap], x_centers, [1 + gap]))
    outer_y_top = (([segment_height], y_tops[:3], [segment_height]))
    outer_y_bottom = (([-segment_height], y_bottoms[3:], [-segment_height]))
    (outer_x, outer_y_top, 'k-', linewidth=segment_width)
    (outer_x, outer_y_bottom, 'k-', linewidth=segment_width)
    ([-1 - gap, 1 + gap], [segment_height, segment_height], 'k-', linewidth=segment_width)
    ([-1 - gap, 1 + gap], [-segment_height, -segment_height], 'k-', linewidth=segment_width)
 
    ax.set_aspect('equal')
    ('off')  # Close the axis 
# Function that displays lettersdef display_letter(letter, ax):
    segments = [int(bit) for bit in segment_codes[letter]]
    draw_seven_segment_display(ax, segments)
 
# Test functiondef main():
    fig, ax = ()
    letter = 'A'  # can be changed to other letters    display_letter(letter, ax)
    (f'Seven Segment Display: {letter}')
    ()
 
if __name__ == '__main__':
    main()

In this example,segment_codesThe dictionary defines the segment code of the letter.draw_seven_segment_displayThe function is responsible for drawing the frame and segments of the seven-segment digital tube based on the segment code.display_letterThe function isdraw_seven_segment_displayencapsulation, it takes a letter as an argument and calls the draw function.mainA function is the entry to the program, which creates a drawing window that displays the specified letters and displays the results.

Through this article, we provide a complete code example with how to use Python to draw a seven-segment digital tube to display letters, from a theoretical overview to a code implementation. I hope these contents will be helpful to readers and can play a role in practical applications.

This is the end of this article about drawing seven-segment digital tube letters in Python. For more related content on drawing seven-segment digital tubes in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!