Pyinstaller loading splash screen
Asked Answered
V

5

11

Pyinstaller recently added a splash screen option (yay!) but the splash stays open the entire time the exe is running. I need it because my file opens very slowly and I want to warn the user not to close the window. Is there a way I can get the splash screen to close when the gui opens?

Verlaverlee answered 20/8, 2021 at 13:28 Comment(0)
P
5

from pyinstaller docs:

import pyi_splash

# Update the text on the splash screen
pyi_splash.update_text("PyInstaller is a great software!")
pyi_splash.update_text("Second time's a charm!")

# Close the splash screen. It does not matter when the call
# to this function is made, the splash screen remains open until
# this function is called or the Python program is terminated.
pyi_splash.close()
Pontiac answered 20/8, 2021 at 13:51 Comment(3)
Where it code goes?Fourier
@Jaime, in the main filePontiac
Better to configure the .spec file properly rather then adding workarounds in the main code. Answer of @pyro is a good example.Branch
P
2
import sys

if getattr(sys, 'frozen', False):
    import pyi_splash

# your code..........

if getattr(sys, 'frozen', False):
    pyi_splash.close()

#root.mainloop()

See: https://coderslegacy.com/python/splash-screen-for-pyinstaller-exe/

(To run splash you must specify during pyinstaller build --splash=The_image_you_choose.png #or .jpg)

Paucity answered 9/7, 2023 at 14:53 Comment(1)
Doesn't work under Windows 11: the splash screen remains open atop of the launched application.Anthropoid
R
1

I had this same issue, and even though this post is old i could not find the answer :) but i was able to figure out a way to make the splash screen go away.

Once your spec file is generated using the '--splash=picture.png' modify the spec file

Parameter to mod

always_on_top=True -> set to always_on_top=False

Then rerun the pyinstaller command using the modified spec file -> pyinstaller main.spec

Rhianna answered 23/6, 2022 at 20:24 Comment(2)
The splash screen doesn't go away, it is just that your application window can open in front of it. If you move your application window you still see it.Destalinization
At least this makes the splash screen go away when the application closes...Khoury
I
0

Another interesting approach could be the following. As described in the Pyinstaller documentation, the pyi_splash module cannot be installed by a package manager because it is part of PyInstaller, so you should put the import statement within a try ... expect block. However, something like

try:
    import pyi_splash
except:
    pass

is an anti-pattern in Python.

Thus, to properly load your splash screen and having it closed when the application starts while being pythonic, you can do the following:

from contextlib import suppress
from PyQt5.QtWidgets import QApplication

from ui import YourAppUI

def main():
    if not QApplication.instance():
        app = QApplication(sys.argv)
    else:
        app = QApplication.instance()

    app.setStyle("Fusion")
    ui = YourAppUI()
    ui.show()
    with suppress(ModuleNotFoundError):
        import pyi_splash  # noqa

        pyi_splash.close()
    app.exec_()


if __name__ == "__main__":
    main()

(# noqa is one of those comments that can be read by your IDE to suppress warnings)

To know more about this context manager, take a look here or in the docs.

Ioab answered 17/4, 2023 at 14:49 Comment(0)
M
0

I´m using this code into my software on windows 10 (pc) and works like a charm, but using the compiled software on Windows 11 (laptop), the splash stay fixed there and wont close... the software runs under splash but can´t use.

Masters answered 14/1 at 14:46 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Culler

© 2022 - 2024 — McMap. All rights reserved.