import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): btn = QPushButton('Click me', self) btn.setGeometry(50, 50, 100, 30) btn.clicked.connect(self.animate) self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Animation Example') self.show() def animate(self): anim = QPropertyAnimation(btn, b'size', self) anim.setDuration(2000) anim.setStartValue(QSize(100, 30)) anim.setEndValue(QSize(150, 50)) anim.setEasingCurve(QEasingCurve.OutBounce) anim.start()
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): lbl = QLabel('Hello', self) lbl.setGeometry(50, 50, 100, 30) lbl.setAlignment(Qt.AlignCenter) lbl.setStyleSheet('QLabel { background-color: yellow }') lbl.mousePressEvent = self.animate self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Animation Example') self.show() def animate(self, event): anim = QPropertyAnimation(lbl, b'color', self) anim.setDuration(2000) anim.setStartValue(QColor(255, 255, 0)) anim.setEndValue(QColor(255, 0, 0)) anim.setEasingCurve(QEasingCurve.InOutCubic) anim.start() QTimer.singleShot(2000, lambda: lbl.setStyleSheet('QLabel { background-color: yellow }'))In this example, we create a QLabel object and set its background color to yellow using the setStyleSheet() method. We then override the mousePressEvent() method to trigger the custom animate() method. Inside the animate() method, we create a QPropertyAnimation object and set its target object (lbl), property (color), duration (2000ms), start and end values (QColor objects), and easing curve (InOutCubic). We then start the animation with the start() method, and set a QTimer to revert the background color back to yellow after 2000ms.