[英]“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.