Create Splash screen in PyQt5

Question:

I want to create a splash screen in PyQt5 using Python. I searched but I found in Pyqt4 and I have no understanding of PyQt4 so help me in this case I would be gratful

Splash screen in pyqt

Asked By: sabeen kanwal

||

Answers:

Try it:

import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout, QApplication, QSplashScreen 
from PyQt5.QtCore import QTimer

class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)

        self.b1 = QPushButton('Display screensaver')
        self.b1.clicked.connect(self.flashSplash)

        layout = QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self.b1)

    def flashSplash(self):
        self.splash = QSplashScreen(QPixmap('D:/_Qt/img/pyqt.jpg'))

        # By default, SplashScreen will be in the center of the screen.
        # You can move it to a specific location if you want:
        # self.splash.move(10,10)

        self.splash.show()

        # Close SplashScreen after 2 seconds (2000 ms)
        QTimer.singleShot(2000, self.splash.close)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Dialog()
    main.show()
    sys.exit(app.exec_())

enter image description here


Example 2

import sys
from PyQt5 import QtCore, QtGui, QtWidgets     # + QtWidgets


import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore    import QTimer, Qt

if __name__ == '__main__':
    app = QApplication(sys.argv)

    label = QLabel("""
            <font color=red size=128>
               <b>Hello PyQt, The window will disappear after 5 seconds!</b>
            </font>""")

    # SplashScreen - Indicates that the window is a splash screen. This is the default type for .QSplashScreen
    # FramelessWindowHint - Creates a borderless window. The user cannot move or resize the borderless window through the window system.
    label.setWindowFlags(Qt.SplashScreen | Qt.FramelessWindowHint)
    label.show()

    # Automatically exit after  5 seconds
    QTimer.singleShot(5000, app.quit) 
    sys.exit(app.exec_())

enter image description here

Answered By: S. Nick

I like to add it in just before i load my main widget with a slight fade – note this is only useful to show a logo, if your application has a long load time you can utilise the splash screen like @S. Nick has shown to allow load time whilst you show the splashscreen:

from PyQt5 import QtGui, QtCore, QtWidgets
import time

if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    # Create splashscreen
    splash_pix = QtGui.QPixmap('picture.png')
    splash = QtWidgets.QSplashScreen(splash_pix, QtCore.Qt.WindowStaysOnTopHint)
    # add fade to splashscreen 
    opaqueness = 0.0
    step = 0.1
    splash.setWindowOpacity(opaqueness)
    splash.show()
    while opaqueness < 1:
        splash.setWindowOpacity(opaqueness)
        time.sleep(step) # Gradually appears
        opaqueness+=step
    time.sleep(1) # hold image on screen for a while
    splash.close() # close the splash screen
    #widget = YourWidget()
    #widget.show() # This is where you'd run the normal application
    app.exec_()
Answered By: Liam

The simple and best example that I found.

https://www.youtube.com/watch?v=TsatZJfzb_Q&t=162s

from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog
from PyQt5.QtWidgets import QGraphicsScene,QSplashScreen
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap

class SplashScreen(QSplashScreen):
    def __init__(self):
        super(QSplashScreen, self).__init__()
        loadUi("splash.ui", self)
        self.setWindowFlag(Qt.FramelessWindowHint)
        pixmap = QPixmap("any_image.jpg")
        self.setPixmap(pixmap)

    def progress(self):
        for i in range(40):
            time.sleep(0.1)
            self.progressBar.setValue(i)

class MainScreen(QMainWindow):
    def function1():
       .......
    def function2():
       ......
    
if __name__ == "__main__":
    app = QApplication(sys.argv)

    splash = SplashScreen()
    splash.show()
    splash.progress()

    mainscreen = MainScreen()
    mainscreen.show()

    splash.finish(widget)

    sys.exit(app.exec_())
Answered By: Barun Basnet