'Create splash screen with GIF and image

I want to have a splash screen that contains an image and a GIF (that gif will be on top of the image).

I have already done that, But I think it is not a good approach and I killed the idea of using Splash screen since I used a timer.

How can I avoid using a timer.

Here is my last code:


import time

from PyQt5.QtCore import QRect, QMetaObject, QCoreApplication, Qt
from PyQt5.QtGui import QMovie, QPixmap
from PyQt5.QtWidgets import QHBoxLayout, QLabel, QApplication, QWidget


class Ui_Form(QWidget):

    def __init__(self, parent=None):
        super(Ui_Form, self).__init__(parent)
        self.setObjectName("self")
        self.resize(400, 400)
        self.label = QLabel(self)
        self.label.setText("This is main application")
        self.label.setObjectName("label")

def showStartScreen():
    start = time.time()

    # PNG image
    img_path = f".../splash_image.PNG"
    image = QPixmap(img_path)

    # set layout in order to put GIF in above (on top) a word that are in splash image
    layout = QHBoxLayout()

    # big image for Splash screen
    image_container = QLabel()
    image_container.setWindowFlag(Qt.SplashScreen, Qt.FramelessWindowHint)
    image_container.setLayout(layout)
    image_container.setPixmap(image)

    # label for displaying GIF
    label_2 = QLabel()
    label_2.setStyleSheet("margin-bottom:21px; margin-right:21px")

    movie = QMovie(".../loading_text_border_animation.gif")
    label_2.setMovie(movie)

    layout.addWidget(label_2, 0, Qt.AlignRight | Qt.AlignBottom)
    movie.start()
    image_container.show()

    # My problem is here, As you can say i made a timer for 5s in order to keep that GIF moving,
    # so making splash screen here is pointless, because
    # for example if the app needs only 3s to start then we slow it down by 2s
    while time.time() < start + 5:
        app.processEvents()


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    ui = Ui_Form()
    showStartScreen()
    ui.show()
    sys.exit(app.exec_())


Solution 1:[1]

One possible solution is to use QTimer:

    image_container = QLabel()
    image_container.setAttribute(Qt.WA_DeleteOnClose) // <--
    image_container.setWindowFlag(Qt.SplashScreen, Qt.FramelessWindowHint)
    image_container.setLayout(layout)
    image_container.setPixmap(image)

    # ...

    layout.addWidget(label_2, 0, Qt.AlignRight | Qt.AlignBottom)
    movie.start()
    image_container.show()

    return image_container


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    ui = Ui_Form()
    splash_screen = showStartScreen()
    QTimer.singleShot(5 * 1000, splash_screen.close)
    QTimer.singleShot(5000, ui.show)
    sys.exit(app.exec_())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eyllanesc