繁体   English   中英

PyQT5中的“闪烁”按钮

[英]“Blinking” buttons in PyQT5

这就是问题所在:我试图在按下按钮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获得代码本身,并在此处和此处进行了一些修改以测试我的需求。 它的主要部分很简单:我创建了一个带有两个QPushButton的窗口,一个启动QPropertyAnimation ,另一个启动它。 QPropertyAnimation本身应用于按钮之一。 理论上,它应该更改按钮的背景颜色,但是由于在另一个问题中解释的限制,它仅为QPushButton提供了彩色边框。 而且我很好,它看起来并不那么令人讨厌。

问题

启动动画后,如果按“ Stop按钮,则该按钮不会返回到其原始状态(无彩色边框),而是保持按下“ Stop按钮时的动画颜色。

此外,我得到以下警告:

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

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

问题

如何正确“重置”按钮的styleSheet? 还有另一种(也许更好,更Pythonic)的方法来做闪烁的按钮吗? 非常感谢!

该属性不会保存初始状态,因此即使您将动画配对,也不会将其还原到初始状态。 因此,这种情况下的解决方案是将该状态保存在变量中,并创建一个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_()

暂无
暂无

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

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