Pyinstaller 最近添加了一个启动画面选项(耶!),但是这个启动画面会在可执行文件运行期间一直保持打开状态。我需要这个功能,因为我的文件打开非常缓慢,我想警告用户不要关闭窗口。有没有办法让启动画面在GUI打开时关闭?
Pyinstaller 最近添加了一个启动画面选项(耶!),但是这个启动画面会在可执行文件运行期间一直保持打开状态。我需要这个功能,因为我的文件打开非常缓慢,我想警告用户不要关闭窗口。有没有办法让启动画面在GUI打开时关闭?
来自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()
我曾经遇到过同样的问题,即使这篇文章已经很老了,我仍然找不到答案 :) 但是我能够想出一种方法让启动画面消失。
使用“--splash=picture.png”生成您的规范文件后,修改规范文件
要修改的参数
always_on_top=True -> 设置为always_on_top=False
然后使用修改后的规范文件重新运行pyinstaller命令 -> pyinstaller main.spec
import sys
if getattr(sys, 'frozen', False):
import pyi_splash
# your code..........
if getattr(sys, 'frozen', False):
pyi_splash.close()
#root.mainloop()
--splash=The_image_you_choose.png
#或 .jpg)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 读取以抑制警告)