我确实运行了一些耗时的计算,并希望在进程运行时显示启动画面。我试过了:
form = QSpashScreen()
form.showMessage('Please wait ...')
form.show()
time_consuming_function()
form.close()
但实际上并没有出现闪屏。我怎样才能做到这一点?
答案 0 :(得分:0)
您需要为QSplashScreen指定一个父级,否则它将在本机Qt中映射此构造函数:
QSplashScreen::QSplashScreen ( const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0 )
Construct a splash screen that will display the pixmap.
There should be no need to set the widget flags, f, except perhaps Qt::WindowStaysOnTopHint.
如果您没有此窗口小部件,则可以将QDesktopWidget指定为父窗口
from PyQt4.QtGui import *
import time
def time_consuming_function():
time.sleep(10)
def main():
app = QApplication(sys.argv)
form = QSplashScreen(QDesktopWidget().screen())
form.showMessage('Please wait ...')
form.resize(1000, 1000)
form.move(0, 0)
form.show()
time_consuming_function()
form.close()
app.exec_()
if __name__ == '__main__':
import sys
main()
顺便说一句,你不应该将“tiem_consuming_function”与main的相同线程一起使用,这将阻止UI线程,使UI线程无法响应用户输入,看起来UI正在冻结。