在 pyqt 的启动画面中使用 gif [英] Using a gif in Splash Screen in pyqt
本文介绍了在 pyqt 的启动画面中使用 gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是使用 pyqt 进行 gui 设计的新手.我想在启动画面中使用 gif.. 使用此代码..
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_()
但我只得到了 gif 的第一帧...我该如何克服这个问题...?
But i am getting only the first frame of the gif...How do i overcome this...?
推荐答案
这是我得到的:
#!/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_()
请注意,您需要在单独的线程中运行初始化代码,因为主线程应该调度 Qt 事件.
Note, that you need to run your initialization code in a separate thread, since the main thread should dispatch Qt events.
这篇关于在 pyqt 的启动画面中使用 gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文