анимация pyqt приводит к частичному исчезновению объекта - PullRequest
0 голосов
/ 20 февраля 2019

Я создавал анимацию в pyqt с мячом.Но когда я увеличиваю скорость анимации, мяч частично разочаровывает.Что я могу сделать сейчас?Пожалуйста, помогите!

Мой код:

from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPixmap, QPainterPath
from PyQt5.QtCore import QPointF, QPropertyAnimation, pyqtProperty
import sys


class Ball(QLabel):
    def __init__(self, parent):
        super().__init__(parent)

        pix = QPixmap("ball.png")
        self.h = pix.height()
        self.w = pix.width()

        self.setPixmap(pix)

    def _set_pos(self, pos):

        self.move(pos.x() - self.w / 2, pos.y() - self.h / 2)

    pos = pyqtProperty(QPointF, fset=_set_pos)


class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.ball = Ball(self)
        self.path = QPainterPath()
        self.path.moveTo(400, 30)
        self.path.lineTo(900, 30)
        self.setWindowTitle("ball animation")
        self.setGeometry(300, 300, 1000, 300)
        self.anim()

    def anim(self):
        self.anima = QPropertyAnimation(self.ball, b"pos")
        self.anima.setDuration(3000)
        self.anima.setStartValue(QPointF(30, 30))
        self.anima.setEndValue(QPointF(900, 30))
        self.anima.finished.connect(self.anim)
        self.anima.start()


app = QApplication(sys.argv)
mw = Example()
mw.show()
sys.exit(app.exec())

Когда я запускаю это, я получаю что-то вроде этого:

my program

1 Ответ

0 голосов
/ 21 февраля 2019

Изменение размера немного QLabel ширина

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui     import QPixmap, QPainterPath
from PyQt5.QtCore    import QPointF, QPropertyAnimation, pyqtProperty

class Ball(QLabel):
    def __init__(self, parent):
        super().__init__(parent)

        pix = QPixmap("ball.png")
        self.h = pix.height()
        self.w = pix.width()

        self.resize(self.w+5, self.h)                  # <---------------
# or 
#        self.setContentsMargins(0, 0, 5, 0)           # <---------------

        self.setPixmap(pix)

    def _set_pos(self, pos):

        self.move(pos.x() - self.w / 2, pos.y() - self.h / 2)

    pos = pyqtProperty(QPointF, fset=_set_pos)


class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.ball = Ball(self)
        self.path = QPainterPath()
        self.path.moveTo(400, 30)
        self.path.lineTo(900, 30)
        self.setWindowTitle("ball animation")
        self.setGeometry(300, 300, 1000, 300)
        self.anim()

    def anim(self):
        self.anima = QPropertyAnimation(self.ball, b"pos")
        self.anima.setDuration(3000)
        self.anima.setStartValue(QPointF(30, 30))
        self.anima.setEndValue(QPointF(900, 30))
        self.anima.finished.connect(self.anim)
        self.anima.start()


app = QApplication(sys.argv)
mw = Example()
mw.show()
sys.exit(app.exec())

ball.png

enter image description here

...