Python 按钮未在Qt4中设置动画

Python 按钮未在Qt4中设置动画,python,pyqt,Python,Pyqt,我是这个论坛的新手,也是Python和PyQt的新手。 在学习了一些基础知识之后,在我尝试使用Qt学习动画的过程中,我希望编写一个简单的应用程序,在这个应用程序中,单击另一个按钮,按钮就会从屏幕左侧动画地移动到某个位置。在此过程中,我最终编写了如下代码: class AppMainUI(QtGui.QMainWindow, Ui_MainWindow): def __init__(self): QtGui.QMainWindow.__init__(self)

我是这个论坛的新手,也是Python和PyQt的新手。 在学习了一些基础知识之后,在我尝试使用Qt学习动画的过程中,我希望编写一个简单的应用程序,在这个应用程序中,单击另一个按钮,按钮就会从屏幕左侧动画地移动到某个位置。在此过程中,我最终编写了如下代码:

class AppMainUI(QtGui.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.setupUi(self)

    self.setWindowFlags(QtCore.Qt.Window|QtCore.Qt.FramelessWindowHint)
    self.connect(self.pushButton_4, QtCore.SIGNAL("clicked()"), self.onExitClicked)
    self.connect(self.pushButton_5, QtCore.SIGNAL("clicked()"), self.animateButtons)

    def animateButtons(self):
        animation = QtCore.QPropertyAnimation(self.pushButton, "geometry")
        animation.setDuration(2)
        animation.setStartValue(QtCore.QRect(0, 0, self.pushButton.width(), self.pushButton.height()))
        animation.setEndValue(QtCore.QRect(760, 280, self.pushButton.width(), self.pushButton.height()))
        animation.setEasingCurve(QtCore.QEasingCurve.OutElastic)
        animation.start()

    def onExitClicked(self):
        sys.exit(0)

App = QtGui.QApplication(sys.argv)
UI = AppMainUI()
UI.show()
App.exec_()
我看到的是,按钮移动到位置(0,0),这实际上是我的动画开始位置。我在这里做错了什么吗?

问题是由垃圾收集器引起的;当方法
animateButtons(self)
使用
animation.start()
语句完成时,
animation
对象被垃圾收集器销毁,因此动画不会发生

只需在对象名称的开头添加
self

self.animation = QtCore.QPropertyAnimation(...)
self.animation.setStartValue(...)
...

另外,将持续时间增加到至少1000毫秒,以便动画可见,否则执行速度将非常快,您将无法看到它。

嘿,非常感谢……它成功了。谢谢你帮助我!