12、程序启动界面

Par @Martin dans le
Tags :

当程序的初始化工作比较多, 程序可能启动较长时间后, 窗口才会显示出来, 用户没准会抱怨程序响应的慢.
为了改善用户体验, 最好在程序初始化这段时间显示 logo, 或者其他信息提示用户程序已启动, QT 提供了 QSplashScreen 类, 其使用方法比较简单.

The QSplashScreen widget provides a splash screen that can be shown during application startup.

import sys
from PyQt4.QtGui import QApplication, QSplashScreen, QPixmap

app = QApplication(sys.argv)
splash = QSplashScreen(QPixmap(':/splash.png'))  # step1, 创建一个 splash
splash.show()  # step2, 显示 splash
app.processEvents()  # step3, processEvents() 的调用防止 splash 阻塞事件 (you can hide the splash screen by clicking on it with the mouse)
dlg = Dialog()
dlg.show()
splash.finish(dlg)  # step4, 等待 dlg 显示之后关闭 splash (Makes the splash screen wait until the widget mainWin is displayed before calling close() on itself)
sys.exit(app.exec_())


你还可以通过 QSplashScreen 的 showMessage 方法在 splash screen 上打印提示信息:

showMessage (self, QString message, int alignment = Qt.AlignLeft, QColor color = Qt.black)