Я не совсем понял, что вы хотите сделать, но вариант анимации я вам покажу.
Также рекомендую вам познакомиться с QWidget::update()
- обновляет виджет, если обновления не отключены или виджет не скрыт.
Больше https://doc.qt.io/qt-5/qwidget.html#update
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class CirclingWidget(QWidget):
offset = QPoint()
def __init__(self, diameter=1, relative=True, sprite=None,
anglesPerSecond=360, clockwise=True, offset=None, alignment=None):
super().__init__()
self.diameter = diameter
self.relative = relative
self.clockwise = clockwise
self.setSprite(sprite, offset or alignment)
self.animation = QVariantAnimation(startValue=0., endValue=1.,
duration=360000 / anglesPerSecond, loopCount=-1)
self.animation.valueChanged.connect(self.update)
def setSprite(self, sprite, arg):
if isinstance(sprite, str):
sprite = QPixmap(sprite)
if isinstance(sprite, QPixmap) and not sprite.isNull():
self.sprite = sprite
if isinstance(arg, Qt.AlignmentFlag):
self.setAlignment(arg)
else:
self.setOffset(arg)
else:
self.sprite = None
self.update()
def setAlignment(self, alignment=None):
if self.sprite:
x = y = 0
if alignment is not None:
if alignment & Qt.AlignHCenter:
x = -self.sprite.width() / 2
elif alignment & Qt.AlignRight:
x = -self.sprite.width()
if alignment & Qt.AlignVCenter:
y = -self.sprite.height() / 2
elif alignment & Qt.AlignBottom:
y = -self.sprite.height()
self.offset = QPointF(x, y)
self.update()
def setOffset(self, offset=None):
if self.sprite:
x = y = 0
if isinstance(offset, int):
x = y = offset
elif isinstance(offset, float):
x = self.sprite.width() * offset
y = self.sprite.height() * offset
elif isinstance(offset, (QPoint, QPointF)):
x = offset.x()
y = offset.y()
elif isinstance(offset, (tuple, list)):
x, y = offset
self.offset = QPointF(x, y)
self.update()
def setAnglesPerSecond(self, ratio):
self.animation.setDuration(360000 / ratio)
def setClockwise(self, clockwise=True):
if self.clockwise != clockwise:
self.clockwise = clockwise
self.animation.setCurrentTime(
self.animation.duration() - self.animation.currentTime())
self.update()
def start(self, angle=0):
self.animation.start()
# угол считается по часовой стрелке, начиная с 12ч
start = (angle - 90) / 360 % 1
self.animation.setCurrentTime(start * self.animation.duration())
self.update()
def stop(self):
self.animation.stop()
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHints(painter.Antialiasing)
painter.setPen(QPen(Qt.black, 1, Qt.SolidLine))
size = self.diameter
if self.relative:
size *= min(self.width(), self.height())
rect = QRectF(0, 0, size, size)
rect.moveCenter(QRectF(self.rect()).center())
path = QPainterPath()
path.addEllipse(rect)
painter.drawPath(path)
if self.sprite:
pos = self.animation.currentValue()
if not self.clockwise:
pos = 1 - pos
pos = path.pointAtPercent(pos)
painter.drawPixmap(pos + self.offset, self.sprite)
class MainWindow(QtWidgets.QMainWindow):
d = 0
def __init__(self):
super().__init__()
self.circleWidget = CirclingWidget(.7, sprite='NLO.png', # NLO.png
anglesPerSecond=60, alignment=Qt.AlignCenter)
self.setCentralWidget(self.circleWidget)
self.circleWidget.start(90)
def paintEvent(self, event):
painter = QPainter(self)
radialGrad = QRadialGradient(0.3, 0.7, 0.05)
radialGrad.setCoordinateMode(QGradient.ObjectBoundingMode)
radialGrad.setSpread(QGradient.ReflectSpread)
radialGrad.setColorAt(0, QColor('#0250c5'))
radialGrad.setColorAt(1, QColor('#2575fc'))
painter.setBrush(radialGrad)
painter.drawRect(self.rect())
painter.setBrush(QBrush(Qt.white))
painter.setPen(QtGui.QPen(QtCore.Qt.red, -1))
painter.drawRect(100, self.height()-12, 1, 1)
painter.drawRect(200, self.height()-12+self.d, 2, 2)
painter.drawRect(300, self.height()-12, 3, 3)
self.d -= 1
if self.d < -400:
self.d = 0
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(600, 600)
w.show()
sys.exit(app.exec_())
NLO.png