最近在忙点东西,刚好遇到了这个问题
解决了,特此记录一下:
代码:
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtCore import Qt, QPoint, QPropertyAnimation
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.label = QLabel("Hello World!", self)
self.label.setAlignment(Qt.AlignCenter)
self.label.setGeometry(50, 50, 100, 50)
self.animation = QPropertyAnimation(self.label, b"pos")
self.animation.setDuration(2000)
self.animation.setStartValue(QPoint(50, 50))
self.animation.setEndValue(QPoint(250, 250))
self.animation.start()
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
这里要注意,在类里面animation
一定要设为self.animation
,这样使用时才会有效果。