Pyinstaller 加载闪屏界面

8

Pyinstaller 最近添加了一个启动画面选项(耶!),但是这个启动画面会在可执行文件运行期间一直保持打开状态。我需要这个功能,因为我的文件打开非常缓慢,我想警告用户不要关闭窗口。有没有办法让启动画面在GUI打开时关闭?

4个回答

5

来自pyinstaller文档:

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()

代码放在哪里? - Jaime
@Jaime,在主文件中。 - ozs
最好正确配置.spec文件,而不是在主代码中添加解决方法。@pyro的回答是一个很好的例子。 - Ivy Growing

1

我曾经遇到过同样的问题,即使这篇文章已经很老了,我仍然找不到答案 :) 但是我能够想出一种方法让启动画面消失。

使用“--splash=picture.png”生成您的规范文件后,修改规范文件

要修改的参数

always_on_top=True -> 设置为always_on_top=False

然后使用修改后的规范文件重新运行pyinstaller命令 -> pyinstaller main.spec


启动画面没有消失,只是您的应用程序窗口可以在其前面打开。如果您移动应用程序窗口,仍然可以看到它。 - tinman
至少这样在应用关闭时可以让闪屏消失... - Dgomn D

1
import sys

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

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

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

#root.mainloop()


请参考:https://coderslegacy.com/python/splash-screen-for-pyinstaller-exe/ (要运行启动画面,您必须在pyinstaller构建过程中指定 --splash=The_image_you_choose.png #或 .jpg)

0
另一个有趣的方法可能是以下内容。正如Pyinstaller文档中所述,pyi_splash模块无法通过软件包管理器安装,因为它是PyInstaller的一部分,因此您应该将导入语句放在try ... except块内。但是,类似以下代码:
try:
    import pyi_splash
except:
    pass

在Python中,是一种反模式。

因此,为了正确加载您的启动屏幕并在应用程序启动时关闭它,同时保持Pythonic,您可以执行以下操作:

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 是一种注释,可以被你的 IDE 读取以抑制警告)

要了解更多关于这个上下文管理器的信息,请查看这里文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接