先自定义一个控件,增加一个color方法:
class MyBtn(QPushButton):
def __init__(self, text):
super().__init__(text)
def _set_color(self, rgbCol):
self.setStyleSheet(f"background-color: " + rgbCol.name())
color = pyqtProperty(QColor, fset=_set_color)
创建并运行动画:
self.anim = QPropertyAnimation(self.pushButton, b"color")
self.anim.setDuration(3000)
self.anim.setStartValue(QColor(255, 50, 50, 50)) # 粉色
self.anim.setKeyValueAt(0.5, QColor(255, 0, 0, 250)) # 红色
self.anim.setEndValue(QColor(255, 250, 50, 50)) # 米黄
self.anim.start()
全部代码:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MyBtn(QPushButton):
def __init__(self, parent):
super().__init__(parent)
def _set_color(self, rgbCol):
self.setStyleSheet(f"backgr