Skip to content

Instantly share code, notes, and snippets.

@justinfx
Last active December 19, 2022 01:53
Show Gist options
  • Save justinfx/5318065 to your computer and use it in GitHub Desktop.
Save justinfx/5318065 to your computer and use it in GitHub Desktop.
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_()
from PySide2 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.resize(300,200)
layout = QtWidgets.QVBoxLayout(self)
self.label = QtWidgets.QLabel("Some sample text")
font = self.label.font()
font.setPointSize(20)
self.label.setFont(font)
layout.addWidget(self.label)
self.button = QtWidgets.QPushButton("Start", self)
layout.addWidget(self.button)
self.animation = anim = QtCore.QPropertyAnimation(self, b"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 = QtWidgets.QApplication([])
w = Widget()
w.show()
app.exec_()
@AdhTri001
Copy link

Awesome dude!

@zartyblartfast
Copy link

Tried Pip Install PySide...
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
only these python versions are supported: [(2, 6), (2, 7), (3, 2), (3, 3), (3, 4)]

@justinfx
Copy link
Author

justinfx commented Dec 18, 2022

@zartyblartfast this isn't really related to the code. Seems you were having trouble trying to install the older PySide 1.x in combination with having a newer python3 installed. I have added a new PySide2 version of the code so you can install PySide2 on your system

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