Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of how to flash the color of the text of a QLabel in PySide or PyQt4. Uses QPropertyAnimation with a custom setColor property.
from PySide import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.resize(300,200)
layout = QtGui.QVBoxLayout(self)
self.label = QtGui.QLabel("Some sample text")
font = self.label.font()
font.setPointSize(20)
self.label.setFont(font)
layout.addWidget(self.label)
self.button = QtGui.QPushButton("Start", self)
layout.addWidget(self.button)
self.animation = anim = QtCore.QPropertyAnimation(self, "color", self)
anim.setDuration(250)
anim.setLoopCount(2)
anim.setStartValue(self.color)
anim.setEndValue(self.color)
anim.setKeyValueAt(0.5, QtGui.QColor(0,255,0))
self.button.clicked.connect(anim.start)
def getColor(self):
return self.label.palette().text()
def setColor(self, color):
palette = self.label.palette()
palette.setColor(self.label.foregroundRole(), color)
self.label.setPalette(palette)
color = QtCore.Property(QtGui.QColor, getColor, setColor)
if __name__ == "__main__":
app = QtGui.QApplication([])
w = Widget()
w.show()
app.exec_()
@AdhTri001
Copy link

AdhTri001 commented Sep 27, 2021

Awesome dude!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment