Skip to content
Advertisement

is there any way to pass existing label to another class to make it blink

  • I have some labels which I want to make them blink in some cases, and stop blinking with specific color for their styleSheet on other cases.
  • I have seen this useful answer, and adapted this part from the answer:
class AnimatedLabel(QLabel):
    def __init__(self):
        QLabel.__init__(self)

        color_red = QColor(200, 0, 0)
        color_green = QColor(0, 200, 0)
        color_blue = QColor(0, 0, 200)
        color_yellow = QColor(255, 255, 100)
        #
        color_lightgreen = QColor(100, 200, 100)
        color_pink = QColor(200, 100, 100)

        self.color_anim = QPropertyAnimation(self, b'zcolor')
        self.color_anim.setStartValue(color_yellow)
        self.color_anim.setKeyValueAt(0.4, color_yellow)
        self.color_anim.setKeyValueAt(0.6, color_lightgreen)
        self.color_anim.setEndValue(color_yellow)
        self.color_anim.setDuration(2000)
        self.color_anim.setLoopCount(-1)

    def parseStyleSheet(self):
        ss = self.styleSheet()
        sts = [s.strip() for s in ss.split(';') if len(s.strip())]
        return sts

    def getBackColor(self):

        return self.palette().color(self.pal_ele)

    def setBackColor(self, color):
        sss = self.parseStyleSheet()
        bg_new = 'background-color: rgba(%d,%d,%d,%d);' % (color.red(), color.green(), color.blue(), color.alpha())

        for k, sty in enumerate(sss):
            if re.search('Abackground-color:', sty):
                sss[k] = bg_new
                break
        else:
            sss.append(bg_new)

        self.setStyleSheet('; '.join(sss))

    pal_ele = QPalette.Window
    zcolor = pyqtProperty(QColor, getBackColor, setBackColor)

I have created some labels on my main window with specific geometry and etc like:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        #------------------------------------------------------
        # Main Window
        #==============
        MainWindow.resize(1200, 800)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        # ArUco Group Box
        self.arucoGB = QtWidgets.QGroupBox(self.centralwidget)
        self.arucoGB.setGeometry(QRect(260, 140, 841, 631))
        self.arucoGB.setObjectName("arucoGB")
        self.arucoGB.setStyleSheet("background-color: lightgreen; border: 1px solid black;")
        # 07
        self.LB_07 = QLabel(self.arucoGB)
        self.LB_07.setGeometry(QRect(420, 190, 131, 121))
        self.LB_07.setObjectName("LB_07")
        self.LB_07.setStyleSheet("background-color: green; border: 1px solid black;")
        MainWindow.setCentralWidget(self.centralwidget)
        #------------------------------------------------
        self.retranslateUi(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "ArUco_Tasks"))
        self.arucoGB.setTitle(_translate("MainWindow", "Platform"))
        self.LB_07.setText(_translate("MainWindow", "07"))

My Question is:

How can I pass the label from the MainWindow class and the foreground, background colors to the first class to be animated in two modes blinking/non-blinking? thanks in advance.

Edit:

here is the final GUI shape, and the squares are the labels I want to make them blink. enter image description here

Advertisement

Answer

Thanks to @eyllanesc and @musicamante for their valuable notes the solution was:

class AnimatedLabel(QLabel):
    def __init__(self, parent=None):
        QLabel.__init__(self, parent)

        self.colorDict = {
                "b": QColor(0, 0, 255),
                "y": QColor(255, 255, 0),
                "g": QColor(0, 130, 0),
                "bg": [QColor(0, 0, 255), QColor(0, 130, 0)],
                "yg": [QColor(255, 255, 0), QColor(0, 130, 0)],
                "bp": [QColor(0, 0, 255), QColor(255, 10, 10)],
                "yp": [QColor(255, 255, 0), QColor(255, 10, 10)]
                }
        self.color_anim = QPropertyAnimation(self, b'zcolor')

    def blink(self, mode):
        
        self.color_anim.stop()

        # No Blinking
        if len(mode)==1:
            bgColor = fgColor = self.colorDict[mode]
        # Blinking
        elif len(mode)==2:
            bgColor = self.colorDict[mode][0]
            fgColor = self.colorDict[mode][1]
        # wrong mode
        else:
            bgColor = fgColor  = QColor(0, 0, 0)

        self.color_anim.setStartValue(bgColor)
        self.color_anim.setKeyValueAt(0.2, bgColor)
        self.color_anim.setKeyValueAt(0.6, fgColor)
        self.color_anim.setKeyValueAt(0.2, bgColor)
        self.color_anim.setEndValue(bgColor)
        self.color_anim.setDuration(2000)
        self.color_anim.setLoopCount(-1)
        self.color_anim.start()

    def parseStyleSheet(self):
        ss = self.styleSheet()
        sts = [s.strip() for s in ss.split(';') if len(s.strip())]
        return sts

    def getBackColor(self):
        return self.palette().color(self.pal_ele)

    def setBackColor(self, color):
        sss = self.parseStyleSheet()
        bg_new = 'background-color: rgba(%d,%d,%d,%d);' % (color.red(), color.green(), color.blue(), color.alpha())

        for k, sty in enumerate(sss):
            if re.search('Abackground-color:', sty):
                sss[k] = bg_new
                break
        else:
            sss.append(bg_new)

        self.setStyleSheet('; '.join(sss))

    pal_ele = QPalette.Window
    zcolor = QtCore.pyqtProperty(QColor, getBackColor, setBackColor)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        #------------------------------------------------------
        # Main Window
        #==============
        MainWindow.resize(1200, 800)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        # ArUco Group Box
        self.arucoGB = QtWidgets.QGroupBox(self.centralwidget)
        self.arucoGB.setGeometry(QRect(260, 140, 841, 631))
        self.arucoGB.setObjectName("arucoGB")
        self.arucoGB.setStyleSheet("background-color: lightgreen; border: 1px solid black;")
        # 07
        self.LB_07 = AnimatedLabel(self.arucoGB)
        self.LB_07.setGeometry(QRect(420, 190, 131, 121))
        self.LB_07.setObjectName("LB_07")
        self.LB_07.setStyleSheet("background-color: green; border: 1px solid black;")
        MainWindow.setCentralWidget(self.centralwidget)
        #------------------------------------------------
        self.retranslateUi(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "ArUco_Tasks"))
        self.arucoGB.setTitle(_translate("MainWindow", "Platform"))
        self.LB_07.setText(_translate("MainWindow", "07"))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement