有没有办法将现有标签传递给另一个类以使其闪烁
Posted
技术标签:
【中文标题】有没有办法将现有标签传递给另一个类以使其闪烁【英文标题】:is there any way to pass existing label to another class to make it blink 【发布时间】:2021-07-15 10:28:54 【问题描述】: 我有一些标签,我想让它们在某些情况下闪烁,并在其他情况下停止以特定颜色为它们的styleSheet
闪烁。
我看到了这个有用的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)
我在主窗口上创建了一些带有特定几何图形等的标签,例如:
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"))
我的问题是:
如何将MainWindow
类和foreground, background
颜色中的标签传递给第一个类,以便以两种模式blinking/non-blinking
进行动画处理?提前致谢。
编辑:
这是最终的 GUI 形状,方块是我想让它们闪烁的标签。
【问题讨论】:
为什么不直接把QLabel
改成AnimatedLabel
呢,改成def __init__(self, parent=None):
QLabel.__init__(self, parent)
@Bilal 你已经在做,只需应用 eyllanesc 建议的更改
@musicamante 我现在明白了,谢谢你的留言,我会尝试这样做。
【参考方案1】:
感谢@eyllanesc 和@musicamante 的宝贵意见,解决方案是:
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"))
【讨论】:
以上是关于有没有办法将现有标签传递给另一个类以使其闪烁的主要内容,如果未能解决你的问题,请参考以下文章