Why QSplashscreen does not always work?

前端 未结 1 1660
鱼传尺愫
鱼传尺愫 2020-12-18 15:55

I\'ve just coded splash screen in my PyQt application, to show an image before start. I\'ve used QSplashscreen. The problem is the image is displayed, let\'s say, once in a

相关标签:
1条回答
  • 2020-12-18 16:39

    It's because of the sleep(1) line. For QSplashScreen to work properly, there should be an event loop running. However, sleep is blocking. So you don't get to app.exec_() (event loop) part before sleep finishes (for a whole second). That 'gray rectangle' is the case where you enter sleep before QSplashScreen could even paint itself.

    For the normal case, you won't have this problem because you'll be waiting within Qt and the event loop will be running. If you want to 'simulate' a wait, sleep for small intervals and force the app to do its job with .processEvents():

    # -*- coding: utf-8 -*-
    import sys
    from time import time, sleep
    from PyQt4.QtGui import QApplication, QSplashScreen, QPixmap
    
    from gui.gui import MainWindow
    
    def main():
        app = QApplication(sys.argv)
        start = time() 
        splash = QSplashScreen(QPixmap("aquaticon/images/splash_screen.jpg"))
        splash.show()
        while time() - start < 1:
            sleep(0.001)
            app.processEvents()
        win = MainWindow()
        splash.finish(win)
        win.show()
        app.exec_()
    
    if __name__ == "__main__":
        main()
    
    0 讨论(0)
提交回复
热议问题