【发布时间】:2021-02-25 07:59:42
【问题描述】:
【问题讨论】:
标签: python pyqt pyqt5 qpushbutton
【问题讨论】:
标签: python pyqt pyqt5 qpushbutton
这是一种使用 QTimer 并重新实现 paintEvent 以在 timeout 上绘制半径增加的椭圆的方法。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Button(QPushButton):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.r = 0
self.timer = QTimer(interval=25, timeout=self.set_radius)
self.clicked.connect(self.timer.start)
def set_radius(self):
if self.r < self.width() / 2:
self.r += self.width() / 20
else:
self.timer.stop()
self.r = 0
self.update()
def paintEvent(self, event):
super().paintEvent(event)
if self.r:
qp = QPainter(self)
qp.setBrush(QColor(255, 255, 255, 127))
qp.setPen(Qt.NoPen)
qp.drawEllipse(self.rect().center(), self.r, self.r)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
layout.addWidget(Button('Start'))
window.setStyleSheet('''
QPushButton {
color: #fff;
background-color: #09e;
padding: 20px;
font-size: 24pt;
border: none;
}
QPushButton:pressed {
color: #ddd;
}''')
window.show()
sys.exit(app.exec_())
【讨论】:
如果你想实现任何动画,那么你必须使用继承自 QAbstractAnimation 的类来平滑地实现属性的变化。
在以下示例中,我使用 QVariantAnimation:
import sys
from PyQt5.QtCore import Qt, QVariantAnimation
from PyQt5.QtGui import QColor, QPainter
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
class Button(QPushButton):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._radius = 0
self._animation = QVariantAnimation(startValue=0.0)
self._animation.valueChanged.connect(self._handle_valueChanged)
self._animation.finished.connect(self._handle_finished)
self._animation.setDuration(300)
self.pressed.connect(self._start_animation)
def _start_animation(self):
self._animation.setEndValue(self.width() / 2.0)
self._animation.start()
def _handle_valueChanged(self, value):
self._radius = value
self.update()
def _handle_finished(self):
self._radius = 0
self.update()
def paintEvent(self, event):
super().paintEvent(event)
if self._radius:
qp = QPainter(self)
qp.setBrush(QColor(255, 255, 255, 127))
qp.setPen(Qt.NoPen)
qp.drawEllipse(self.rect().center(), self._radius, self._radius)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
layout.addWidget(Button("Start"))
window.setStyleSheet(
"""
QPushButton {
color: #fff;
background-color: #09e;
padding: 20px;
font-size: 24pt;
border: none;
}
QPushButton:pressed {
color: #ddd;
}"""
)
window.show()
sys.exit(app.exec_())
【讨论】:
试试这个:)
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Button(QPushButton):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.r = 0
self.timer = QTimer(interval=70, timeout=self.set_radius)
self.countr = 0
def set_radius(self):
self.countr += 1
if self.r < self.width() / 2:
self.r += self.width() /10
else:
self.timer.stop()
self.r = 0
self.countr = 0
self.update()
def mousePressEvent(self,event):
super().mousePressEvent(event)
self.x,self.y = event.x(),event.y()
self.timer.start()
def paintEvent(self, event):
super().paintEvent(event)
if self.r:
self.qp = QPainter(self)
self.qp.setBrush(QColor(255, 255, 255, 127))
self.qp.setRenderHint(QPainter.Antialiasing)
# delete this lines if don't like light effect
#///
if self.countr == 4:
self.qp.setOpacity(0.8)
else:
self.qp.setOpacity(0.3)
self.qp.setPen(QPen(QColor(Qt.transparent), 0))
#\\\
#self.qp.setOpacity(0.3)
#self.qp.setPen(QPen(QColor(Qt.transparent), 0))
self.qp.drawEllipse(QPoint(self.x,self.y), self.r, self.r)
self.qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
layout.addWidget(Button('Start'))
window.setStyleSheet('''
QPushButton {
color: #fff;
background-color: #09e;
padding: 20px;
font-size: 24pt;
border: none;
}
QPushButton:pressed {
color: #ddd;
}''')
window.show()
sys.exit(app.exec_())
【讨论】: