“闪烁"PyQT5 中的按钮 [英] "Blinking" buttons in PyQT5

查看:144
本文介绍了“闪烁"PyQT5 中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

交易如下:我试图让按钮 A 在按下按钮 B 时闪烁",然后当用户按下按钮 A 时闪烁应该停止.此外,按钮 A 应该会返回到之前的状态.

Here's the deal: I'm trying to make button A "blink" when button B is pushed, and the blinking should then stop when the user pushes button A. Furthermore, button A should then go back to its former state.

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

After long reading through the documentation and other questions at SO, I have now following piece of code:

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 本身应用于按钮之一.理论上它应该改变按钮的背景颜色,但由于 this other question 它只为 QPushButton 提供了一个彩色边框.而且我很满意,它看起来并不那么烦人.

The code itself I got mostly from @Alexander Lutsenko in this question, with a few modifications here and there to test my needs. The main part ist straightforward: I create a window with two QPushButton, one to start the QPropertyAnimation and the other one to stop it. The QPropertyAnimation itself is applied to one of the buttons. It should in theory change the background color of the button, but due to the limitations explained in this other question it only provides a colorful border to the QPushButton. And I'm fine with that, it doesn´t look that intrusive.

问题

动画开始后,如果我按下Stop按钮,按钮不会回到原来的状态(没有彩色边框),而是保持动画的颜色停止按钮被按下的时间.

After starting the animation, if I push the Stop button, the button doesn't go back to its original state (with no colorful border), but stays with the color of the animation at the time the Stop button was pressed.

此外,我收到以下警告:

Furthermore, I get following warning:

TypeError: 无法将 Python 'QBrush' 对象转换为 C++ 'QColor' 实例

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

But the script keeps running and the animation going, so that doesn't break anything.

问题

如何正确重置"按钮的样式表?是否有另一种(可能更好的 Pythonic)方法来执行闪烁按钮的操作?非常感谢!

How do I "reset" the styleSheet of the button properly? Is there another (maybe better and pythonic) way to do the blinking button stuff? Thanks a lot!

推荐答案

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

The property does not save the initial state so even if you pair the animation it will not be restored to the initial state. So the solution in this case is to save that state in a variable and create a reset_color() method that restores the color again..

另一方面,错误信息:TypeError: cannot convert to Python 'QBrush' object to a C++ 'QColor' instance 表示代码 self.button_stop.palette().base() 返回一个QBrush,但它是一个QColor,并且没有隐含的转换,因此必须改变实现.

On the other hand the error message: TypeError: unable to convert to Python 'QBrush' object to a C ++ 'QColor' instance indicates that the code self.button_stop.palette().base() returns a QBrush, but it is expected a QColor, and there is no conversion implied, so that implementation must be changed.

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

As a matter of order I will create a new class that inherits from QPushButton and implement those properties.

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_()

这篇关于“闪烁"PyQT5 中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆