forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GifSplashScreen.py
86 lines (64 loc) · 2.43 KB
/
GifSplashScreen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2020/6/11
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file:
@description:
"""
from time import sleep
try:
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QMovie
from PyQt5.QtWidgets import QApplication, QSplashScreen, QWidget
except ImportError:
from PySide2.QtCore import Qt
from PySide2.QtGui import QMovie
from PySide2.QtWidgets import QApplication, QSplashScreen, QWidget
class GifSplashScreen(QSplashScreen):
def __init__(self, *args, **kwargs):
super(GifSplashScreen, self).__init__(*args, **kwargs)
self.movie = QMovie('Data/splash.gif')
self.movie.frameChanged.connect(self.onFrameChanged)
self.movie.start()
def onFrameChanged(self, _):
self.setPixmap(self.movie.currentPixmap())
def finish(self, widget):
self.movie.stop()
super(GifSplashScreen, self).finish(widget)
class BusyWindow(QWidget):
def __init__(self, *args, **kwargs):
super(BusyWindow, self).__init__(*args, **kwargs)
# 模拟耗时操作,一般来说耗时的加载数据应该放到线程
for i in range(5):
sleep(1)
splash.showMessage('加载进度: %d' % i, Qt.AlignHCenter | Qt.AlignBottom, Qt.white)
QApplication.instance().processEvents()
splash.showMessage('初始化完成', Qt.AlignHCenter | Qt.AlignBottom, Qt.white)
splash.finish(self)
if __name__ == '__main__':
import sys
import cgitb
cgitb.enable(format='text')
app = QApplication(sys.argv)
global splash
splash = GifSplashScreen()
splash.show()
w = BusyWindow()
w.show()
# 测试二
# def createWindow():
# app.w = QWidget()
# # 模拟初始5秒后再显示
# splash.showMessage('等待界面显示', Qt.AlignHCenter | Qt.AlignBottom, Qt.white)
# QTimer.singleShot(3000, lambda: (
# splash.showMessage('初始化完成', Qt.AlignHCenter | Qt.AlignBottom, Qt.white), app.w.show(),
# splash.finish(app.w)))
# 模拟耗时5秒。但是不能用sleep
# 可以使用子线程加载耗时的数据
# 主线程中循环设置UI可以配合QApplication.instance().processEvents()
# QTimer.singleShot(3000, createWindow)
splash.showMessage('等待创建界面', Qt.AlignHCenter | Qt.AlignBottom, Qt.white)
sys.exit(app.exec_())