PyQt5 Simple Status Light#
In practice, it is necessary to obtain the device status in real time and display it in the software in the form of a status light. One simple and convenient method is to use the Label
widget in PyQt5
, modify the label to a circular shape, and fill it with different colors.
- Design of the status light: Set the size of the label to a square shape, currently using a length of 20 and a width of 20. Set the
setStyleSheet
to a circular shape with a diameter of 20 and a radius of 10, and default to red.
The code is as follows:
self.StdWlrStatus = QtWidgets.QLabel(self.centralwidget) # Create a new label
self.StdWlrStatus.setGeometry(QtCore.QRect(35, 110, 20, 20)) # Set the size of the label to 20x20
self.StdWlrStatus.setStyleSheet("border-radius:10px;background-color:red") # Set the label to a circular shape with red color
- Based on conditions, determine the device status and assign different colors to the status light.
def StdStatusLightSet(self, ThreadSingDic):
SheetStrHead = "border-radius:10px;background-color:"
if ThreadSingDic["Connect"] == 1: # Check if the MWR device is online, 1 for online, 0 for offline, -1 for connection exception
WLRStatus = "green"
else:
WLRStatus = "red"
self.StdWlrStatus.setStyleSheet(SheetStrHead + WLRStatus)
Actual usage of the status light: