Is it possible to add MPEG as a splash screen (start up screen)?
-
Hello, I would like to have some animation on the startup window (which will end till the GUIs get ready) instead of a typical image.
And since the animation is hard to implement with pyqt5( I don't have experience), I would like to use a MPEG format.
I would appreciate any tutorials that can help me in implementation as well.
-
Hi,
You should take a look at the QSplashScreen implementation.
You would need to add the QtMultimedia module to your application which might be a bit overkill for just the splash screen.
-
@SGaist Thanks for your answer, Yes, you're right, I noticed that using a Video is not recommended.
i have many options, I wish that you can help me with them by telling me which is better:- Using MPEG and load it by using QtMultimedia -------------- [Canceled)
- is it the same with GIF if I loaded it by using QLabel -> setMovie(QMovie)
- Or displaying PNG sequences
- or draw it with pyqt5 (but I don't think I can do so)
- or stay away from using animation in the splash screen and display a normal image.
-
What kind of animation do you have in mind ?
-
Hi @Pythonic-person,
Using QMultimedia can work, but need that QApplication.processEvents() is called permantently from the GUI thread for the video to be displayed correctly, so this is a little bit counter productive as the goal is to show the splashcreen during the loading process. But you can imagine loading everything in a separated thread or manage to call processEvents frequently for that.
Anyway here is some basic example of a "proof of concept" I did for showing a video in a splashscreen. It is done with PySide6 but except the imports, the code should be quite the same. Hopefully it can help
from typing import Optional from PySide6.QtMultimedia import QMediaPlayer from PySide6.QtMultimediaWidgets import QVideoWidget from PySide6.QtWidgets import QWidget, QBoxLayout, QSplashScreen, QMainWindow, QLabel from PySide6.QtGui import QScreen from PySide6.QtCore import Qt, QUrl, QElapsedTimer class VideoPlayerWidget(QWidget): '''Widget for playing videos''' def __init__(self, path: str, parent: Optional[QWidget] = None) -> None: super().__init__(parent) self.path = path self.__initui__() def __initui__(self): self.setWindowFlags(Qt.FramelessWindowHint | Qt.WA_TranslucentBackground) self._player = QMediaPlayer() self._player.setSource(QUrl(self.path)) self._videoWidget = QVideoWidget() self._player.setVideoOutput(self._videoWidget) self._player.play() self._player.setLoops(QMediaPlayer.Infinite) l = QBoxLayout(QBoxLayout.TopToBottom) l.setContentsMargins(0,0,0,0) l.addWidget(self._videoWidget) self.setLayout(l) class VideoSplash(QSplashScreen): ''' Splash screen playing video''' def __init__(self, path: str, screen:Optional[QScreen]=None, parent: Optional[QWidget] = None) -> None: super().__init__(screen=screen, parent=parent) self.path = path self.__initui__() def __initui__(self): '''''' l = QBoxLayout(QBoxLayout.TopToBottom) l.addWidget(VideoPlayerWidget(path=self.path)) l.setContentsMargins(0,0,0,0) self.setLayout(l) self.resize(1920/2, 1080/2) class MyWindow(QMainWindow): '''''' def __init__(self, parent: Optional[QWidget] = None, flags: Qt.WindowFlags = Qt.WindowFlags()) -> None: super().__init__(parent, flags) content = QLabel('Main Window') f = content.font() f.setPointSize(20) f.setBold(True) content.setFont(f) self.setCentralWidget(content) self.wait() def wait(self, timeout=3000) -> None: ''' Sleep few seconds for faking heavy loading process ''' elapsed = QElapsedTimer() elapsed.start() while(elapsed.elapsed() < timeout): QApplication.processEvents() if __name__ == "__main__": from PySide6.QtWidgets import QApplication import sys a = QApplication(sys.argv) path = "C:/path/to/my/video.mp4" s = VideoSplash(path=path) screen:QScreen = QApplication.screens()[0] # move at center s.move((screen.size().width() - s.size().width()) / 2, (screen.size().height() - s.size().height()) / 2) s.show() QApplication.processEvents() w = MyWindow() s.finish(w) w.show() a.exec()