update splash add sleep demo

This commit is contained in:
Irony 2020-11-20 09:53:09 +08:00
parent feb5a9eff1
commit e6821d219d

View file

@ -14,7 +14,9 @@ __Author__ = 'Irony'
__Copyright__ = 'Copyright (c) 2020'
__Version__ = 'Version 1.0'
from PyQt5.QtCore import QTimer, Qt
from time import sleep
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QMovie
from PyQt5.QtWidgets import QApplication, QSplashScreen, QWidget
@ -35,6 +37,20 @@ class GifSplashScreen(QSplashScreen):
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
@ -42,23 +58,28 @@ if __name__ == '__main__':
cgitb.enable(1, None, 5, '')
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)))
# 测试二
# 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)
QTimer.singleShot(3000, createWindow)
sys.exit(app.exec_())