maandoo maandoo - 5 months ago 162
Python Question

Using a gif in Splash Screen in pyqt

I am new to gui designing with pyqt..
I want to use a gif in the splash screen.. using this code..

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class Form(QDialog):

def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.browser = QTextBrowser()
self.setWindowTitle('Just a dialog')

if __name__ == "__main__":
import sys, time

app = QApplication(sys.argv)

# Create and display the splash screen
splash_pix = QPixmap('a.gif')
splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
splash.setMask(splash_pix.mask())
#splash.raise_()
splash.show()
app.processEvents()

# Simulate something that takes time
time.sleep(2)

form = Form()
form.show()
splash.finish(form)
app.exec_()


But i am getting only the first frame of the gif...How do i overcome this...?

Answer

Here what I got:

#!/usr/bin/env python2
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from multiprocessing import Pool

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QTextBrowser()
        self.setWindowTitle('Just a dialog')

class MySplashScreen(QSplashScreen):
    def __init__(self, animation, flags):
        # run event dispatching in another thread
        QSplashScreen.__init__(self, QPixmap(), flags)
        self.movie = QMovie(animation)
        self.connect(self.movie, SIGNAL('frameChanged(int)'), SLOT('onNextFrame()'))
        self.movie.start()

    @pyqtSlot()
    def onNextFrame(self):
        pixmap = self.movie.currentPixmap()
        self.setPixmap(pixmap)
        self.setMask(pixmap.mask())

# Put your initialization code here
def longInitialization(arg):
    time.sleep(arg)
    return 0
if __name__ == "__main__":
    import sys, time

    app = QApplication(sys.argv)

    # Create and display the splash screen
#   splash_pix = QPixmap('a.gif')
    splash = MySplashScreen('a.gif', Qt.WindowStaysOnTopHint)
#   splash.setMask(splash_pix.mask())
    #splash.raise_()
    splash.show()
    app.processEvents()

    # this event loop is needed for dispatching of Qt events
    initLoop = QEventLoop()
    pool = Pool(processes=1)
    pool.apply_async(longInitialization, [2], callback=lambda exitCode: initLoop.exit(exitCode))
    initLoop.exec_()

    form = Form()
    form.show()
    splash.finish(form)
    app.exec_()

Note, that you need to run your initialization code in a separate thread, since the main thread should dispatch Qt events.

Comments