如何解决有什么方法可以将现有标签传递给另一个类以使其闪烁
class AnimatedLabel(QLabel):
def __init__(self):
QLabel.__init__(self)
color_red = QColor(200,0)
color_green = QColor(0,200,0)
color_blue = QColor(0,200)
color_yellow = QColor(255,255,100)
#
color_lightgreen = QColor(100,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);' % (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 形状,方块是我想让它们闪烁的标签。
解决方法
感谢@eyllanesc 和@musicamante 的宝贵笔记,解决方案是:
class AnimatedLabel(QLabel):
def __init__(self,parent=None):
QLabel.__init__(self,parent)
self.colorDict = {
"b": QColor(0,255),"y": QColor(255,255,0),"g": QColor(0,130,"bg": [QColor(0,QColor(0,0)],"yg": [QColor(255,"bp": [QColor(0,QColor(255,10,10)],"yp": [QColor(255,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)
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);' % (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"))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。