pyqt5 改变控件背景色的动画

先自定义一个控件,增加一个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"background-color: " + rgbCol.name())

    color = pyqtProperty(QColor, fset=_set_color)


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.resize(400, 300)
        self.btn = MyBtn(self)
        self.btn.setText('背景色变换测试用按钮')

        self.anim = QPropertyAnimation(self.btn, 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()


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    myWin = MainWindow()
    myWin.show()
    sys.exit(app.exec_())

按这思路其实可以完成很多变换,就不举例了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 QLabel 背景有黑到白变化的动画效果,可以使用 QTimer 和 QPropertyAnimation 类来控制 QLabel 的背景色动画。 具体步骤如下: 1. 创建一个 QTimer 对象,用于定时触发背景色动画。 ``` timer = QTimer() ``` 2. 创建一个 QPropertyAnimation 对象,用于控制 QLabel 的背景色动画。 ``` animation = QPropertyAnimation(label, b"styleSheet") ``` 其中,`label` 表示要进行动画的 QLabel 对象,`b"styleSheet"` 表示对 QLabel 的样式表属性进行动画。 3. 设置 QTimer 的定时器间隔和启动动画。 ``` timer.setInterval(2000) timer.timeout.connect(animation.start) timer.start() ``` 其中,`setInterval` 方法设置定时器的间隔时间,`timeout` 信号连接到 `start` 方法,表示定时器超时时启动动画。 4. 设置 QPropertyAnimation 的动画属性和值。 ``` animation.setDuration(1000) animation.setStartValue("background-color: #000000") animation.setEndValue("background-color: #FFFFFF") ``` 其中,`setDuration` 方法设置动画的持续时间,`setStartValue` 方法设置动画的起始样式表,`setEndValue` 方法设置动画的结束样式表。 5. 在动画结束时,更新 QLabel 的样式表。 ``` animation.finished.connect(lambda: label.setStyleSheet("background-color: #FFFFFF")) ``` 其中,`finished` 信号连接到一个 lambda 函数,该函数在动画结束时更新 QLabel 的样式表。 最终的代码如下: ``` from PyQt5.QtCore import QTimer, QPropertyAnimation from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("Hello, World!") label.setStyleSheet("background-color: #000000; border: 2px solid #cccccc; border-radius: 10px;") timer = QTimer() animation = QPropertyAnimation(label, b"styleSheet") animation.setDuration(1000) animation.setStartValue("background-color: #000000") animation.setEndValue("background-color: #FFFFFF") animation.finished.connect(lambda: label.setStyleSheet("background-color: #FFFFFF")) timer.setInterval(2000) timer.timeout.connect(animation.start) timer.start() label.show() app.exec_() ``` 以上代码会显示一个带有黑到白背景色变化动画效果的 QLabel 控件。每隔 2 秒钟,控件背景色会从黑色变化到白色,并在变化结束时更新样式表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值