Skip to content

Commit e6821d2

Browse files
committed
update splash add sleep demo
1 parent feb5a9e commit e6821d2

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

QSplashScreen/GifSplashScreen.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
__Copyright__ = 'Copyright (c) 2020'
1515
__Version__ = 'Version 1.0'
1616

17-
from PyQt5.QtCore import QTimer, Qt
17+
from time import sleep
18+
19+
from PyQt5.QtCore import Qt
1820
from PyQt5.QtGui import QMovie
1921
from PyQt5.QtWidgets import QApplication, QSplashScreen, QWidget
2022

@@ -35,30 +37,49 @@ def finish(self, widget):
3537
super(GifSplashScreen, self).finish(widget)
3638

3739

40+
class BusyWindow(QWidget):
41+
42+
def __init__(self, *args, **kwargs):
43+
super(BusyWindow, self).__init__(*args, **kwargs)
44+
# 模拟耗时操作,一般来说耗时的加载数据应该放到线程
45+
for i in range(5):
46+
sleep(1)
47+
splash.showMessage('加载进度: %d' % i, Qt.AlignHCenter | Qt.AlignBottom, Qt.white)
48+
QApplication.instance().processEvents()
49+
50+
splash.showMessage('初始化完成', Qt.AlignHCenter | Qt.AlignBottom, Qt.white)
51+
splash.finish(self)
52+
53+
3854
if __name__ == '__main__':
3955
import sys
4056
import cgitb
4157

4258
cgitb.enable(1, None, 5, '')
4359

4460
app = QApplication(sys.argv)
61+
62+
global splash
4563
splash = GifSplashScreen()
4664
splash.show()
4765

66+
w = BusyWindow()
67+
w.show()
4868

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-
69+
# 测试二
70+
# def createWindow():
71+
# app.w = QWidget()
72+
# # 模拟初始5秒后再显示
73+
# splash.showMessage('等待界面显示', Qt.AlignHCenter | Qt.AlignBottom, Qt.white)
74+
# QTimer.singleShot(3000, lambda: (
75+
# splash.showMessage('初始化完成', Qt.AlignHCenter | Qt.AlignBottom, Qt.white), app.w.show(),
76+
# splash.finish(app.w)))
5777

5878
# 模拟耗时5秒。但是不能用sleep
5979
# 可以使用子线程加载耗时的数据
6080
# 主线程中循环设置UI可以配合QApplication.instance().processEvents()
81+
# QTimer.singleShot(3000, createWindow)
82+
6183
splash.showMessage('等待创建界面', Qt.AlignHCenter | Qt.AlignBottom, Qt.white)
62-
QTimer.singleShot(3000, createWindow)
6384

6485
sys.exit(app.exec_())

0 commit comments

Comments
 (0)