【问题标题】:"Blinking" buttons in PyQT5PyQT5 中的“闪烁”按钮
【发布时间】:2018-11-16 10:05:54
【问题描述】:

问题是这样的:当按下按钮 B 时,我试图让按钮 A“闪烁”,然后当用户按下按钮 A 时闪烁应该停止。此外,按钮 A 应该回到原来的状态。

在阅读了 SO 的文档和其他问题后,我现在有了以下代码:

Python 2.7

class Widget(QWidget):

    def __init__(self):
        super(Widget, self).__init__()

        self.resize(300,200)
        layout = QVBoxLayout(self)


        self.button_stop = QPushButton("Stop")
        layout.addWidget(self.button_stop)

        self.button_start = QPushButton("Start", self)
        layout.addWidget(self.button_start)

        self.animation = QPropertyAnimation(self, "color", self)
        self.animation.setDuration(1000)
        self.animation.setLoopCount(100)
        self.animation.setStartValue(self.color)
        self.animation.setEndValue(self.color)
        self.animation.setKeyValueAt(0.1, QColor(0,255,0))

        self.button_start.clicked.connect(self.animation.start)
        self.button_stop.clicked.connect(lambda: self.stop())

    def stop(self):
        self.animation.stop()
        self.button_stop.setStyleSheet("")

    def getColor(self):
        return self.button_stop.palette().base()

    def setColor(self, color):
        palette = self.button_stop.palette()
        palette.setColor(self.button_stop.backgroundRole(), color)
        self.button_stop.setAutoFillBackground(True)
        self.button_stop.setPalette(palette)


    color = pyqtProperty(QColor, getColor, setColor)


if __name__ == "__main__":
    app = QApplication([])
    w = Widget()
    w.show()
    app.exec_()

我主要从@Alexander Lutsenko 在this 问题中获得的代码本身,在这里和那里进行了一些修改以测试我的需求。主要部分很简单:我创建了一个带有两个QPushButton 的窗口,一个用于启动QPropertyAnimation,另一个用于停止它。 QPropertyAnimation 本身应用于其中一个按钮。理论上它应该改变按钮的背景颜色,但由于this other question 中解释的限制,它只为QPushButton 提供了一个彩色边框。我对此很好,它看起来并不那么令人讨厌。

问题

启动动画后,如果我按下Stop按钮,按钮不会回到原来的状态(没有彩色边框),而是保持Stop当时的动画颜色按钮被按下。

此外,我收到以下警告:

TypeError: unable to convert a Python 'QBrush' object to a C++ 'QColor' instance

但是脚本一直在运行,动画还在继续,所以这不会破坏任何东西。

问题

如何正确“重置”按钮的样式表?是否有另一种(可能更好和 pythonic)方法来做闪烁按钮的事情?非常感谢!

【问题讨论】:

    标签: python python-2.7 pyqt qpushbutton qpropertyanimation


    【解决方案1】:

    该属性不保存初始状态,因此即使您将动画配对,它也不会恢复到初始状态。所以这种情况下的解决方案是将该状态保存在一个变量中并创建一个reset_color() 方法来再次恢复颜色..

    另一方面,错误消息:TypeError: unable to convert to Python 'QBrush' object to a C ++ 'QColor' instance 表示代码self.button_stop.palette().base() 返回QBrush,但预期为QColor,并且没有隐含转换,因此必须更改实现。

    按照顺序,我将创建一个继承自 QPushButton 的新类并实现这些属性。

    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    
    class BlinkButton(QPushButton):
        def __init__(self, *args, **kwargs):
            QPushButton.__init__(self, *args, **kwargs)
            self.default_color = self.getColor()
    
        def getColor(self):
            return self.palette().color(QPalette.Button)
    
        def setColor(self, value):
            if value == self.getColor():
                return
            palette = self.palette()
            palette.setColor(self.backgroundRole(), value)
            self.setAutoFillBackground(True)
            self.setPalette(palette)
    
        def reset_color(self):
            self.setColor(self.default_color)
    
        color = pyqtProperty(QColor, getColor, setColor)
    
    
    class Widget(QWidget):
    
        def __init__(self):
            super(Widget, self).__init__()
    
            self.resize(300,200)
            layout = QVBoxLayout(self)
    
            self.button_stop = BlinkButton("Stop")
            layout.addWidget(self.button_stop)
    
            self.button_start = QPushButton("Start", self)
            layout.addWidget(self.button_start)
    
            self.animation = QPropertyAnimation(self.button_stop, "color", self)
            self.animation.setDuration(1000)
            self.animation.setLoopCount(100)
            self.animation.setStartValue(self.button_stop.default_color)
            self.animation.setEndValue(self.button_stop.default_color)
            self.animation.setKeyValueAt(0.1, QColor(0,255,0))
    
            self.button_start.clicked.connect(self.animation.start)
            self.button_stop.clicked.connect(self.stop)
    
        def stop(self):
            self.animation.stop()
            self.button_stop.reset_color()
    
    if __name__ == "__main__":
        app = QApplication([])
        w = Widget()
        w.show()
        app.exec_()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 2018-01-30
      • 1970-01-01
      相关资源
      最近更新 更多