Пример 1:
from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton,
QLabel, QHBoxLayout, QSizePolicy)
from PyQt5.QtGui import QColor
from PyQt5.QtCore import QPropertyAnimation, pyqtProperty
import sys
class MyLabel(QLabel):
def __init__(self, text):
super().__init__(text)
def _set_color(self, col):
palette = self.palette()
palette.setColor(self.foregroundRole(), col)
self.setPalette(palette)
color = pyqtProperty(QColor, fset=_set_color)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
self.button = QPushButton("Start", self)
self.button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
hbox.addWidget(self.button)
hbox.addSpacing(40)
self.label = MyLabel("Hello World")
font = self.label.font()
font.setPointSize(35)
self.label.setFont(font)
hbox.addWidget(self.label)
self.anim = QPropertyAnimation(self.label, b"color")
self.anim.setDuration(2500)
self.anim.setLoopCount(2)
self.anim.setStartValue(QColor(0, 0, 0))
self.anim.setEndValue(QColor(255, 255, 255))
self.button.clicked.connect(self.anim.start)
if __name__ == "__main__":
app = QApplication([])
ex = Example()
ex.setGeometry(300, 300, 380, 250)
ex.setWindowTitle('Color anim')
ex.show()
app.exec_()

Пример 2:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class BlinkButton(QPushButton):
def __init__(self, *args, **kwargs):
QPushButton.__init__(self, *args, **kwargs)
self.default_color = self.getColor()
def getColor(self):
return self.palette().color(QPalette.Button)
def setColor(self, value):
if value == self.getColor():
return
palette = self.palette()
palette.setColor(self.backgroundRole(), value)
self.setAutoFillBackground(True)
self.setPalette(palette)
def reset_color(self):
self.setColor(self.default_color)
color = pyqtProperty(QColor, getColor, setColor)
class Widget(QWidget):
def __init__(self):
super(Widget, self).__init__()
layout = QVBoxLayout(self)
self.button_stop = BlinkButton("Stop")
layout.addWidget(self.button_stop)
self.button_start = QPushButton("Start", self)
layout.addWidget(self.button_start)
self.animation = QPropertyAnimation(self.button_stop, b"color", self)
self.animation.setDuration(1000)
self.animation.setLoopCount(100)
self.animation.setStartValue(self.button_stop.default_color)
self.animation.setEndValue(self.button_stop.default_color)
self.animation.setKeyValueAt(0.1, QColor(0,255,0))
self.button_start.clicked.connect(self.animation.start)
self.button_stop.clicked.connect(self.stop)
def stop(self):
self.animation.stop()
self.button_stop.reset_color()
if __name__ == "__main__":
app = QApplication([])
w = Widget()
w.resize(300,200)
w.show()
app.exec_()
