简体   繁体   English

如何为 QPushButton 的背景颜色设置动画(动态更改按钮颜色)

[英]How to animate the background color of a QPushButton(change button color dynamically)

I am trying to make a pushButton in pyqt5 Python, change background color - shades, etc. from a light green, to grenn, and then, to dark green.我正在尝试在 pyqt5 Python 中制作一个按钮,将背景颜色 - 阴影等从浅绿色变为绿色,然后变为深绿色。

I have tryied some code, but nothing.我已经尝试了一些代码,但没有。

Here's my code:这是我的代码:

    def colorChange(self,button):     
        self.anim = QPropertyAnimation(button, 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.anim.start()  

    def eventFilter(self, object, event):
        
        print(int(time.time()-self.first))
        if event.type() == QtCore.QEvent.Enter:
            if self.stackedWidget.currentIndex()==0:
                self.pagelabel1.deleteLater() 
                print("Mouse is over the label")
                self.stop = True
                print('program stop is', self.stop)

                pick=random.randrange(0,2)
                print('random:',pick)
                pick=0
                if pick==0:
                    #self.doAnimation(self.right1)
                    self.colorChange(self.right1)
                    
                    #self.right1.setStyleSheet("background-color: lightgreen")
                    self.right1.clicked.connect(self.nextpage)
                else:
                    #self.doAnimation(self.left1)
                    self.colorChange(self.left1)
                    
                    #self.left1.setStyleSheet("background-color: lightgreen")
                    self.left1.clicked.connect(self.nextpage)

I want the eventFilter to starts the animation, and the animation will be the color change of the button.我希望 eventFilter 启动 animation,animation 将是按钮的颜色变化。

Buttons (and other widgets) don't have a qproperty called color so you should not use QPropertyAnimation, in this case you should use Qt StyleSheet to change the color so you should use a QVariantAnimation:按钮(和其他小部件)没有名为 color 的 qproperty,因此您不应该使用 QPropertyAnimation,在这种情况下,您应该使用 Qt StyleSheet 来更改颜色,因此您应该使用 QVariantAnimation:

import functools
from PyQt5 import QtCore, QtGui, QtWidgets


def helper_function(widget, color):
    widget.setStyleSheet("background-color: {}".format(color.name()))


def apply_color_animation(widget, start_color, end_color, duration=1000):
    anim = QtCore.QVariantAnimation(
        widget,
        duration=duration,
        startValue=start_color,
        endValue=end_color,
        loopCount=2,
    )
    anim.valueChanged.connect(functools.partial(helper_function, widget))
    anim.start(QtCore.QAbstractAnimation.DeleteWhenStopped)


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.button = QtWidgets.QPushButton()

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.button)

        timer = QtCore.QTimer(self, interval=5 * 1000)
        timer.timeout.connect(self.handle_timeout)
        timer.start()
        self.handle_timeout()

    def handle_timeout(self):
        apply_color_animation(
            self.button,
            QtGui.QColor("lightgreen"),
            QtGui.QColor("darkgreen"),
            duration=2500,
        )


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    w = Widget()
    w.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM