14
14
__Copyright__ = 'Copyright (c) 2020'
15
15
__Version__ = 'Version 1.0'
16
16
17
- from PyQt5 .QtCore import QTimer , Qt
17
+ from time import sleep
18
+
19
+ from PyQt5 .QtCore import Qt
18
20
from PyQt5 .QtGui import QMovie
19
21
from PyQt5 .QtWidgets import QApplication , QSplashScreen , QWidget
20
22
@@ -35,30 +37,49 @@ def finish(self, widget):
35
37
super (GifSplashScreen , self ).finish (widget )
36
38
37
39
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
+
38
54
if __name__ == '__main__' :
39
55
import sys
40
56
import cgitb
41
57
42
58
cgitb .enable (1 , None , 5 , '' )
43
59
44
60
app = QApplication (sys .argv )
61
+
62
+ global splash
45
63
splash = GifSplashScreen ()
46
64
splash .show ()
47
65
66
+ w = BusyWindow ()
67
+ w .show ()
48
68
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)))
57
77
58
78
# 模拟耗时5秒。但是不能用sleep
59
79
# 可以使用子线程加载耗时的数据
60
80
# 主线程中循环设置UI可以配合QApplication.instance().processEvents()
81
+ # QTimer.singleShot(3000, createWindow)
82
+
61
83
splash .showMessage ('等待创建界面' , Qt .AlignHCenter | Qt .AlignBottom , Qt .white )
62
- QTimer .singleShot (3000 , createWindow )
63
84
64
85
sys .exit (app .exec_ ())
0 commit comments