In modern electronic display technology, seven-segment digital tube is a widely used display devices, commonly used to display numbers, letters and some special symbols. The basic principle is to control the brightness of the seven segments (a, b, c, d, e, f, g) to form different display contents. In this article, we will introduce in detail how to use Python to draw a seven-segment digital tube to display letters, from the basic principles, design ideas to code implementation, provide complete code examples, and discuss its significance in practical applications.
I. Basic principle of seven-segment digital tube
The seven-segment digital tube consists of seven light-emitting diode (LED) segments, which are: a, b, c, d, e, f, g. Each segment can be independently controlled to light up or down, and by combining the light-up and down states of different segments, it can display the numbers 0-9, part of the alphabet and some special symbols.
There are two common types of seven-segment digital tubes: common anode and common cathode. The common terminal of a common anode digital tube is connected to a high level, and the segments are illuminated by making them connected to a low level; the common terminal of a common cathode digital tube is connected to a low level, and the segments are illuminated by making them connected to a high level.
II. Design thinking
In order to draw a seven-segment digital pipe displaying letters in Python, we need to complete the following steps:
- Define the segment code of the letter: The way each letter is displayed on a seven-segment digital tube is determined by the combination of light and dark in a particular segment. We need to define the corresponding segment code for each letter.
- Frame for drawing a seven-segment digital tube: Use the Graphics Library to draw the frame of a seven-segment digital tube, with each segment represented by a line segment.
- Plotting letters based on segment codes: According to the segment code of the letter, the corresponding segment is lit, i.e. the corresponding line segment is drawn.
- Show letters: Through the function interface, input letters and call the drawing function to display the corresponding seven-segment digital tube pattern.
III. Python code implementation
To draw a seven-segment digital pipe, we can use Python's graphics libraries, such asmatplotlib
maybeturtle
. Here we usematplotlib
to realize.
1. Importing the necessary libraries
import as plt
import numpy as np
2. Defining segment codes for letters
The segment code of a seven-segment digital tube can be represented by a 7-bit binary number, each bit corresponds to a segment, 1 means light, 0 means off.
# Segment codes defining letters, common anode approach (1 for extinction and 0 for illumination in segment codes)
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 add them as needed
}
# Convert segment codes to arrays for plotting
def code_to_segments(code).
return [int(bit) == 0 for bit in code[::-1]] # Invert the binary string, 0 is light, 1 is light.
3. Framework for drawing seven-segment digital tubes
def draw_seven_segment_display(ax, segments, segment_width=0.1, segment_height=0.5, gap=0.02):
# Setting 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
# Drawing of seven segments
for i, segment in enumerate(segments):
if segment:
if i < 3: # top 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: # bottom 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:
# Drawing dotted lines to indicate extinguished segments
if i < 3: # top 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: # bottom 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)
# Drawing Borders
([-1, 1, 1, -1, -1], [0.5, 0.5, -0.5, -0.5, 0.5], 'k-', linewidth=segment_width)
4. Display of 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 Axis
# test function
def main():
fig, ax = ()
letter = 'A'
display_letter(letter, ax)
(f'Seven Segment Display: {letter}')
()
if __name__ == '__main__':
main()
IV. Code Analysis
-
Segment Code Definition:
segment_codes
The dictionary defines the segment code corresponding to each letter. A 1 in the segment code means out and a 0 means on, which is the common anode method. If you are using the common cathode method, you need to swap the 0 and 1 in the segment code. -
batch code conversion:
code_to_segments
The function converts the segment code into a boolean array for drawing, each element in the array corresponds to the lighted or darkened state of a segment. -
Drawing the framework:
draw_seven_segment_display
The function draws the frame and segments of the seven-segment digital tube according to the incoming segment state array. The bright segments are drawn with solid lines and the extinguished segments are drawn with dashed lines (which can be modified as needed). -
Show letters:
display_letter
The function calls the draw function based on the letter and sets the title of the graph. -
test function:
main
function creates a drawing window, calls the function that displays the letters, and displays the result.
V. Practical applications and extensions
-
dynamic display: This can be done through animation libraries such as
) Realize dynamic display of seven-segment digital tubes, such as displaying scrolling numbers or changing letters.
-
hardware control: In combination with GPIO libraries such as
) controls the actual seven-segment digital tube hardware and deploys Python code to the embedded system to realize the dynamic display on the hardware.
- extended character set: The segment code dictionary can be extended to support more characters and symbols, such as lowercase letters, punctuation marks, and so on.
-
Graphical User Interace (GUI) (computing): Using graphical interface libraries such as
tkinter
、PyQt
) creates a user-friendly interface that allows the user to enter characters and display the corresponding seven-segment digital tube pattern.
Here's an example of a program that uses Python andmatplotlib
Code example for the library to draw a seven-segment digital pipe displaying letters of the alphabet. This example will define the segment code of some letters and draw the corresponding seven-segment digital pipe pattern.
First, make sure you have installed thematplotlib
Library. If it is not installed, you can use the following command to install it:
bash copy code
pip install matplotlib
Then, use the following code to draw a 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,0indicate,1express an extinction)
# The segment code sequence is:a, b, c, d, e, f, g
segment_codes = {
'A': '0111111',
'B': '1011110',
'C': '1100110',
'D': '1101101',
'E': '1111101',
'F': '1111001',
'G': '1101111',
# More letters can be added as needed
}
# Functions for drawing seven-segment digital tubes
def draw_seven_segment_display(ax, segments, segment_width=0.1, segment_height=0.6, gap=0.02):
# Setting 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
# Drawing of seven segments
for i, segment in enumerate(segments):
if segment == 0: # Duan Liang
if i < 3: # upper part
([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: # bottom 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 is extinguished.,Not plotted here or can be plotted as a dotted line to indicate that the
# Drawing 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 Axis
# Functions that display letters
def display_letter(letter, ax):
segments = [int(bit) for bit in segment_codes[letter]]
draw_seven_segment_display(ax, segments)
# test function
def 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, thesegment_codes
Dictionaries define segment codes for letters.draw_seven_segment_display
The function is responsible for drawing the frame and segments of the seven-segment digital tube according to the segment code.display_letter
The function isdraw_seven_segment_display
wrapper, which accepts a letter as an argument and calls the draw function.main
The function is the entry point to the program, it creates a drawing window, displays the specified letter and shows the result.
Through this article, we have detailed how to use Python to draw a seven-segment digital pipe to display the letters of the process, from the theoretical overview to the code implementation, providing a complete code example. We hope that these contents are helpful to readers and can be useful in practical applications.