QFrame nad动画

时间:2019-07-31 14:29:06

标签: python pyqt5

我的QWidget上有一个QFrame,单击QPushButton后,    QFrame从(QRect(0,0,100,100))变为(QRect(600,500,100,    100))

问题是:我想每个x和y都知道QFrame    动画时的位置

from PyQt5.QtWidgets import QWidget, QApplication, QFrame, QPushButton
from PyQt5.QtCore import QPropertyAnimation, QRect
from PyQt5.QtGui import QFont
import sys

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

    def initUI(self):
        self.setWindowTitle("Animation Window")
        self.setGeometry(100, 100, 400, 400)
        self.widgets()
        self.show()

    def widgets(self):
        font = QFont("Times New Roman")
        font.setPixelSize(20)

        self.start = QPushButton("Start", self)
        self.start.setFont(font)
        self.start.setGeometry(100, 100, 100, 50)
        self.start.clicked.connect(self.doAnimation)

        self.frame = QFrame(self)
        self.frame.setStyleSheet("background-color:darkGreen;")
        self.frame.setFrameStyle(QFrame.Panel | QFrame.Raised)
        self.frame.setGeometry(250, 100, 100, 100)

    def doAnimation(self):
        self.anim = QPropertyAnimation(self.frame, b"geometry")
        self.anim.setDuration(10000)

        self.anim.setStartValue(QRect(0, 0, 100, 100))
        self.anim.setEndValue(QRect(600, 500, 100, 100))
        self.anim.start()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

动画运行时,每次动画值更改时,都会触发信号valueChanged

    def doAnimation(self):
        self.anim = QPropertyAnimation(self.frame, b"geometry")

        self.anim.valueChanged.connect(lambda: print(self.frame.pos()))

        self.anim.setDuration(10000)
        self.anim.setStartValue(QRect(0, 0, 100, 100))
        self.anim.setEndValue(QRect(600, 500, 100, 100))
        self.anim.start()