簡體   English   中英

如何為 QPushButton 的背景顏色設置動畫(動態更改按鈕顏色)

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

我正在嘗試在 pyqt5 Python 中制作一個按鈕,將背景顏色 - 陰影等從淺綠色變為綠色,然后變為深綠色。

我已經嘗試了一些代碼,但沒有。

這是我的代碼:

    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)

我希望 eventFilter 啟動 animation,animation 將是按鈕的顏色變化。

按鈕(和其他小部件)沒有名為 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