- 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:
JavaScript
x
45
45
1
class AnimatedLabel(QLabel):
2
def __init__(self):
3
QLabel.__init__(self)
4
5
color_red = QColor(200, 0, 0)
6
color_green = QColor(0, 200, 0)
7
color_blue = QColor(0, 0, 200)
8
color_yellow = QColor(255, 255, 100)
9
#
10
color_lightgreen = QColor(100, 200, 100)
11
color_pink = QColor(200, 100, 100)
12
13
self.color_anim = QPropertyAnimation(self, b'zcolor')
14
self.color_anim.setStartValue(color_yellow)
15
self.color_anim.setKeyValueAt(0.4, color_yellow)
16
self.color_anim.setKeyValueAt(0.6, color_lightgreen)
17
self.color_anim.setEndValue(color_yellow)
18
self.color_anim.setDuration(2000)
19
self.color_anim.setLoopCount(-1)
20
21
def parseStyleSheet(self):
22
ss = self.styleSheet()
23
sts = [s.strip() for s in ss.split(';') if len(s.strip())]
24
return sts
25
26
def getBackColor(self):
27
28
return self.palette().color(self.pal_ele)
29
30
def setBackColor(self, color):
31
sss = self.parseStyleSheet()
32
bg_new = 'background-color: rgba(%d,%d,%d,%d);' % (color.red(), color.green(), color.blue(), color.alpha())
33
34
for k, sty in enumerate(sss):
35
if re.search('Abackground-color:', sty):
36
sss[k] = bg_new
37
break
38
else:
39
sss.append(bg_new)
40
41
self.setStyleSheet('; '.join(sss))
42
43
pal_ele = QPalette.Window
44
zcolor = pyqtProperty(QColor, getBackColor, setBackColor)
45
I have created some labels on my main window with specific geometry and etc like:
JavaScript
1
29
29
1
class Ui_MainWindow(object):
2
def setupUi(self, MainWindow):
3
MainWindow.setObjectName("MainWindow")
4
#------------------------------------------------------
5
# Main Window
6
#==============
7
MainWindow.resize(1200, 800)
8
self.centralwidget = QWidget(MainWindow)
9
self.centralwidget.setObjectName("centralwidget")
10
# ArUco Group Box
11
self.arucoGB = QtWidgets.QGroupBox(self.centralwidget)
12
self.arucoGB.setGeometry(QRect(260, 140, 841, 631))
13
self.arucoGB.setObjectName("arucoGB")
14
self.arucoGB.setStyleSheet("background-color: lightgreen; border: 1px solid black;")
15
# 07
16
self.LB_07 = QLabel(self.arucoGB)
17
self.LB_07.setGeometry(QRect(420, 190, 131, 121))
18
self.LB_07.setObjectName("LB_07")
19
self.LB_07.setStyleSheet("background-color: green; border: 1px solid black;")
20
MainWindow.setCentralWidget(self.centralwidget)
21
#------------------------------------------------
22
self.retranslateUi(MainWindow)
23
24
def retranslateUi(self, MainWindow):
25
_translate = QCoreApplication.translate
26
MainWindow.setWindowTitle(_translate("MainWindow", "ArUco_Tasks"))
27
self.arucoGB.setTitle(_translate("MainWindow", "Platform"))
28
self.LB_07.setText(_translate("MainWindow", "07"))
29
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.
Advertisement
Answer
Thanks to @eyllanesc and @musicamante for their valuable notes the solution was:
JavaScript
1
92
92
1
class AnimatedLabel(QLabel):
2
def __init__(self, parent=None):
3
QLabel.__init__(self, parent)
4
5
self.colorDict = {
6
"b": QColor(0, 0, 255),
7
"y": QColor(255, 255, 0),
8
"g": QColor(0, 130, 0),
9
"bg": [QColor(0, 0, 255), QColor(0, 130, 0)],
10
"yg": [QColor(255, 255, 0), QColor(0, 130, 0)],
11
"bp": [QColor(0, 0, 255), QColor(255, 10, 10)],
12
"yp": [QColor(255, 255, 0), QColor(255, 10, 10)]
13
}
14
self.color_anim = QPropertyAnimation(self, b'zcolor')
15
16
def blink(self, mode):
17
18
self.color_anim.stop()
19
20
# No Blinking
21
if len(mode)==1:
22
bgColor = fgColor = self.colorDict[mode]
23
# Blinking
24
elif len(mode)==2:
25
bgColor = self.colorDict[mode][0]
26
fgColor = self.colorDict[mode][1]
27
# wrong mode
28
else:
29
bgColor = fgColor = QColor(0, 0, 0)
30
31
self.color_anim.setStartValue(bgColor)
32
self.color_anim.setKeyValueAt(0.2, bgColor)
33
self.color_anim.setKeyValueAt(0.6, fgColor)
34
self.color_anim.setKeyValueAt(0.2, bgColor)
35
self.color_anim.setEndValue(bgColor)
36
self.color_anim.setDuration(2000)
37
self.color_anim.setLoopCount(-1)
38
self.color_anim.start()
39
40
def parseStyleSheet(self):
41
ss = self.styleSheet()
42
sts = [s.strip() for s in ss.split(';') if len(s.strip())]
43
return sts
44
45
def getBackColor(self):
46
return self.palette().color(self.pal_ele)
47
48
def setBackColor(self, color):
49
sss = self.parseStyleSheet()
50
bg_new = 'background-color: rgba(%d,%d,%d,%d);' % (color.red(), color.green(), color.blue(), color.alpha())
51
52
for k, sty in enumerate(sss):
53
if re.search('Abackground-color:', sty):
54
sss[k] = bg_new
55
break
56
else:
57
sss.append(bg_new)
58
59
self.setStyleSheet('; '.join(sss))
60
61
pal_ele = QPalette.Window
62
zcolor = QtCore.pyqtProperty(QColor, getBackColor, setBackColor)
63
64
class Ui_MainWindow(object):
65
def setupUi(self, MainWindow):
66
MainWindow.setObjectName("MainWindow")
67
#------------------------------------------------------
68
# Main Window
69
#==============
70
MainWindow.resize(1200, 800)
71
self.centralwidget = QWidget(MainWindow)
72
self.centralwidget.setObjectName("centralwidget")
73
# ArUco Group Box
74
self.arucoGB = QtWidgets.QGroupBox(self.centralwidget)
75
self.arucoGB.setGeometry(QRect(260, 140, 841, 631))
76
self.arucoGB.setObjectName("arucoGB")
77
self.arucoGB.setStyleSheet("background-color: lightgreen; border: 1px solid black;")
78
# 07
79
self.LB_07 = AnimatedLabel(self.arucoGB)
80
self.LB_07.setGeometry(QRect(420, 190, 131, 121))
81
self.LB_07.setObjectName("LB_07")
82
self.LB_07.setStyleSheet("background-color: green; border: 1px solid black;")
83
MainWindow.setCentralWidget(self.centralwidget)
84
#------------------------------------------------
85
self.retranslateUi(MainWindow)
86
87
def retranslateUi(self, MainWindow):
88
_translate = QCoreApplication.translate
89
MainWindow.setWindowTitle(_translate("MainWindow", "ArUco_Tasks"))
90
self.arucoGB.setTitle(_translate("MainWindow", "Platform"))
91
self.LB_07.setText(_translate("MainWindow", "07"))
92