首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >pyqt动画导致对象部分消失

pyqt动画导致对象部分消失
EN

Stack Overflow用户
提问于 2019-02-21 02:08:27
回答 1查看 78关注 0票数 0

我用pyqt创建了一个带球的动画。但是当我增加动画的速度时,球会部分消失。我现在能做什么?请帮帮我!

我的代码:

代码语言:javascript
复制
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())

当我运行这个的时候,我得到了类似这样的东西:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-21 07:48:41

更改一点大小QLabel宽度

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54792724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档