PyQT5中的“闪烁”按钮

时间:2018-06-06 16:22:02

标签: python python-2.7 pyqt qpushbutton qpropertyanimation

这是交易:当按下按钮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_()

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

问题

启动动画后,如果我按下Stop按钮,该按钮不会返回其原始状态(没有彩色边框),但在{时保持动画颜色按下了{1}}按钮。

此外,我收到以下警告:

Stop

但是脚本继续运行并且动画继续运行,因此不会破坏任何内容。

问题

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

1 个答案:

答案 0 :(得分:0)

该属性不保存初始状态,因此即使您配对动画,它也不会恢复到初始状态。所以这种情况下的解决方案是将该状态保存在变量中并创建一个再次恢复颜色的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_() 的新类并实现这些属性。

lapply(reg_results, function(model) {predict(model, newdata=subset(df,key==1))})
相关问题