Home Software Hardware Misc About

SevenSegmentDisp for PySide6

vs. 0.2 (10/12/2023)

SevenSegmentDisp

This is SevenSegmentDisp, a PySide custom control that simulates a seven-segment display. It is a port from my previous SevenSegmentDisp wxPython control. This one is also beautiful (segments have rounded corners!), fully customizable yet easy to use. With only a few settings you might be able to find something completely to your taste.

Three settings for the control colors are available: the display background color and colors for the lit and blank segments. As for the display geometry, segments width, height and thickness, the gap between segments as well as the separation between digits can all be set.

You can also tilt (shear) the display by arbitrarily choosing the tilt angle.

The display size is whatever is set via setSize, and it will keep its proportions according to the geometry set.

The control can also show dots and/or colons ("." and/or ":") or simply allocate space for them programatically.

Click here to download the code. It is a compressed file containing two python files: sevensegment.py (the control itself), and sevensegmentdemo.py (a small demo that shows all of the capabilities of the control).

The module itself can be executed to show a digital clock. To run it, type

python3 sevensegment.py
To run the full demo:
python3 sevensegmentdemo.py

Tested on PySide 6.5.2 on Python 3.10.12 on Mint (Xfce). The files mentioned here are distributed under the BSD-3-Clause License.

Methods

SetValue(value)

Sets the character to be shown on the control.

The value passed to the control must be a string of arbitrary length. The value passed will be parsed and translated to digits, so the module can find out how to display them. If a digit produces parsing errors, they will be silent ignored and the digit will be converted to an empty space (all segments off). Not all characters in value may be translated to a single digit: for example, a value of "28.:34." contains four digits, and ".28.:34.", five.

Parsing examples:

3 or "3" will display a "3"
"A" all decimal and hex digits are allowed, but also some other ones as per the _opts dict. Please see the code for a complete list.
"3." will display a "3" with a trailing dot (if EnableDot is True)
"3:" will display a "3" with a trailing colon (if EnableColon is True)
"3.:" or "3:." will display a trailing dot and a trailing colon (if EnableDot and EnableColon are True)
"." or " ." will display an empty digit with a trailing dot
":" or " :" will display an empty digit with a trailing colon
".:" or ":." or " .:" or " :." will display an empty digit with a trailing dot and a trailing colon

SetNumberOfDigits(number_of_digits)

Tells the display how many digits should be shown (int).


SetGeometry(**kwargs)

Sets the display geometry. Keywords: "thickness", "width", "height", "separation"; values: int

e.g: SetGeometry(thickness=28, separation=1) or SetGeometry(width=50), etc.


GetGeometry()

returns a dictionary:

{"thickness": segment thickness,
 "width": width of horizontal segments (a, d, g),
 "height": height of vertical segments (b, c, e, f),
 "separation": gap between segments}


SetTilt(value)

Sets the display tilt in degrees (float, int).


GetTilt()

Returns the display tilt in degrees.


SetColors(**kwargs)

Sets the display colors.

Keywords: "background", "segment_on", "segment_off" ; values: QColor

e.g: SetColors(segment_on=QColor(255, 0, 0), background=QColor(1, 0, 0))


GetColors()

returns a dictionary:

{"background": background QColor,
 "segment_on": lit segments QColor,
 "segment_off": blank segments QColor}


SetSpacing(value)

Sets the space between digits (float or int).


GetSpacing()

Returns the space between digits.


EnableDot(digits, bool)

Tells the display that digits should use a trailing "." if bool is True. If bool is False, the specified digits won't display a trailing dot. The argument digits may be a list or a tuple of ints, a single int, or the string "all":

Examples of valid digits:
[0, 1, 4] or (0, 1, 4)
the method will be applied on the 1st, 2nd and 5th digits (digits are 0-based)
[] or ()
the method will be ignored
3 or [3] or (3)
the method will be applied on the 4th digit
"all" the method will be applied on all current digits

Note that, by default, newly created digits don't have (and don't allocate space to) dots or colons. If the number of digits of a display is dynamically increased, the appropriated method has to be called after digit creation on the newly created digits.


IsDotEnabled(digit)

Returns True if EnableDot is set on the passed digit (int).


ReserveDot(digits, bool)

Tells the display that digits should allocate space for a trailing "." (but not show it, unless explicitly Enable'd), if bool is True. If bool is False, the specified digits won't allocate space. See Examples of valid digits.


IsDotReserved(digit)

Returns True if ReserveDot is set on the passed digit (int).


EnableColon(digits, bool)

Tells the display that digits should use a trailing ":". See Examples of valid digits.


IsColonEnabled(digit)

Returns True if EnableColon is set on the passed digit (int).


ReserveColon(digits, bool)

Tells the display that digits should allocate space for a trailing ":" (but not show it, unless explicitly Enable'd), if bool is True. If bool is False, the specified digits won't allocate space. See Examples of valid digits.


IsColonReserved(digit)

Returns True if ReserveColon is set on the passed digit (int).