|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Created on 2020/6/11 |
| 6 | +@author: Irony |
| 7 | +@site: https://pyqt.site https://github.com/PyQt5 |
| 8 | + |
| 9 | +@file: |
| 10 | +@description: |
| 11 | +""" |
| 12 | + |
| 13 | +__Author__ = 'Irony' |
| 14 | +__Copyright__ = 'Copyright (c) 2020' |
| 15 | +__Version__ = 'Version 1.0' |
| 16 | + |
| 17 | +from PyQt5.QtCore import QTimer, Qt |
| 18 | +from PyQt5.QtGui import QMovie |
| 19 | +from PyQt5.QtWidgets import QApplication, QSplashScreen, QWidget |
| 20 | + |
| 21 | + |
| 22 | +class GifSplashScreen(QSplashScreen): |
| 23 | + |
| 24 | + def __init__(self, *args, **kwargs): |
| 25 | + super(GifSplashScreen, self).__init__(*args, **kwargs) |
| 26 | + self.movie = QMovie('Data/splash.gif') |
| 27 | + self.movie.frameChanged.connect(self.onFrameChanged) |
| 28 | + self.movie.start() |
| 29 | + |
| 30 | + def onFrameChanged(self, _): |
| 31 | + self.setPixmap(self.movie.currentPixmap()) |
| 32 | + |
| 33 | + def finish(self, widget): |
| 34 | + self.movie.stop() |
| 35 | + super(GifSplashScreen, self).finish(widget) |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == '__main__': |
| 39 | + import sys |
| 40 | + import cgitb |
| 41 | + |
| 42 | + cgitb.enable(1, None, 5, '') |
| 43 | + |
| 44 | + app = QApplication(sys.argv) |
| 45 | + splash = GifSplashScreen() |
| 46 | + splash.show() |
| 47 | + |
| 48 | + |
| 49 | + def createWindow(): |
| 50 | + app.w = QWidget() |
| 51 | + # 模拟初始5秒后再显示 |
| 52 | + splash.showMessage('等待界面显示', Qt.AlignHCenter | Qt.AlignBottom, Qt.white) |
| 53 | + QTimer.singleShot(3000, lambda: ( |
| 54 | + splash.showMessage('初始化完成', Qt.AlignHCenter | Qt.AlignBottom, Qt.white), app.w.show(), |
| 55 | + splash.finish(app.w))) |
| 56 | + |
| 57 | + |
| 58 | + # 模拟耗时5秒。但是不能用sleep |
| 59 | + # 可以使用子线程加载耗时的数据 |
| 60 | + # 主线程中循环设置UI可以配合QApplication.instance().processEvents() |
| 61 | + splash.showMessage('等待创建界面', Qt.AlignHCenter | Qt.AlignBottom, Qt.white) |
| 62 | + QTimer.singleShot(3000, createWindow) |
| 63 | + |
| 64 | + sys.exit(app.exec_()) |
0 commit comments