在启动时动画窗口

时间:2013-12-06 18:19:58

标签: qt pyqt pyqt4 pyside

我试图在启动时为窗口设置动画,但它似乎不起作用,我已经编写了下面的代码。

from PyQt4 import QtCore,QtGui

import sys

class AnimatedWindow(QtGui.QMainWindow):
    """docstring for AnimatedWindow"""
    def __init__(self):
        super(AnimatedWindow, self).__init__()
        animation = QtCore.QPropertyAnimation(self, "geometry")
        animation.setDuration(10000);
        animation.setStartValue(QtCore.QRect(0, 0, 100, 30));
        animation.setEndValue(QtCore.QRect(250, 250, 100, 30));
        animation.start();


if __name__ == "__main__":
    application = QtGui.QApplication(sys.argv)
    main = AnimatedWindow()
    main.show()
    sys.exit(application.exec_())

1 个答案:

答案 0 :(得分:2)

这段代码的问题是,当你创建一个QPropertyAnimation的对象时,它被animation.start()语句后的python垃圾收集器破坏,因为animation变量是一个局部变量,因此动画不会发生。要解决此问题,您需要将animation作为成员变量(self.animation

以下是更新后的代码:

from PyQt4 import QtCore,QtGui

import sys

class AnimatedWindow(QtGui.QMainWindow):
    """docstring for AnimatedWindow"""
    def __init__(self):
        super(AnimatedWindow, self).__init__()
        self.animation = QtCore.QPropertyAnimation(self, "geometry")
        self.animation.setDuration(1000);
        self.animation.setStartValue(QtCore.QRect(50, 50, 100, 30));
        self.animation.setEndValue(QtCore.QRect(250, 250, 500, 530));
        self.animation.start();

if __name__ == "__main__":
    application = QtGui.QApplication(sys.argv)
    main = AnimatedWindow()
    main.show()
    sys.exit(application.exec_())