Animating a window on startup

I am trying to animate the window in startup but it doesn't seem to work, I have written the code below.

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_())

Answers


The problem with this code is, when you create an object of QPropertyAnimation it is destroyed by the python garbage collector after animation.start() statement as animation variable is a local variable, hence the animation does not take place. To overcome this problem you need to make the animation as member variable (self.animation)

Here is the updated code which works fine:

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_())

Need Your Help

MySQL the whole query fails if result of sub query is NULL

php mysql sql select subquery

I have a situation where I need to generate rows with random data out of the same table's valid data

How to retrieve an url parameter in the Zend Framework?

php zend-framework url-rewriting parameter-passing

I'am trying to retrieve an parameter passed in a url using the zend framework. But of course, it is not working !