简体   繁体   English

Pyinstaller:如何在python中隐藏致命错误GUI

[英]Pyinstaller : How to hide fatal error GUI in python

After building my exe with pyinstaller, sometimes when an exception occurs, i can see a fatal error in a gui : fatalerror image使用 pyinstaller 构建我的 exe 后,有时发生异常时,我可以在 gui 中看到致命错误: fatalerror image

I just want to hide this.我只是想隐藏这一点。

ps :I use Pyinstaller with --windowed option ps:我使用带有--windowed选项的Pyinstaller

Thanks a lot !非常感谢 !

This dialog is displayed by PyInstaller's startup/shutdown code when your application has an uncaught exception.当您的应用程序有未捕获的异常时,此对话框由 PyInstaller 的启动/关闭代码显示。 The best way to suppress it is to catch the exception in your Python code and exit your app normally (by calling sys.exit() or raising SystemExit ).抑制它的最佳方法是捕获 Python 代码中的异常并正常退出应用程序(通过调用sys.exit()或引发SystemExit )。 This could be done with a top-level try...catch around your main(), or around the call that starts your event loop.这可以通过顶级try...catch围绕 main() 或围绕启动事件循环的调用来完成。

I'd also recommend creating a GUI dialog that displays the traceback for the fatal exception in a text box, to make it easier for users to report the error to you.我还建议创建一个 GUI 对话框,在文本框中显示致命异常的回溯,以便用户更轻松地向您报告错误。

I had a similar issue with a root cause involving subprocess.Popen, if you are not using this function (or something similar) then this answer is unlikely to be helpful.我有一个涉及 subprocess.Popen 的根本原因的类似问题,如果你没有使用这个函数(或类似的东西),那么这个答案不太可能有帮助。

I was having a similar issue trying to build my .exe with pyinstaller.我在尝试使用 pyinstaller 构建我的 .exe 时遇到了类似的问题。 I was using the --noconsole flag as well as the --onefile flag with pyinstaller and was getting the "Fatal Error!"我在 pyinstaller 中使用了 --noconsole 标志以及 --onefile 标志,并且收到了“致命错误!” message " returned -1" whenever I tried to execute the resulting .exe file.每当我尝试执行生成的 .exe 文件时,消息“返回 -1”。 My .exe would build work with just the --onefile flag but any combination using the --noconsole flag would return the error, leading me to believe it was the source of my issue.我的 .exe 将只使用 --onefile 标志构建工作,但使用 --noconsole 标志的任何组合都会返回错误,让我相信这是我的问题的根源。

After a little digging I tracked down this answer:经过一番挖掘,我找到了这个答案:

Python subprocess.call() fails when using pythonw.exe 使用 pythonw.exe 时 Python subprocess.call() 失败

Which seemed to indicate the issue was using pipes when using subprocess.Popen with pythonw.exe instead of python.exe.这似乎表明问题是在使用带有 pythonw.exe 而不是 python.exe 的 subprocess.Popen 时使用管道。 It seemed logical to me that pyinstaller with the --noconsole flag would use pythonw.exe instead of python.exe when building the .exe leading me to believe this could apply to my issue.在我看来,带有 --noconsole 标志的 pyinstaller 在构建 .exe 时使用 pythonw.exe 而不是 python.exe 似乎合乎逻辑,这让我相信这可能适用于我的问题。

To test this I refactored by code to output the results of my Subprocess.Popen call to a file, then read in and deleted the file.为了测试这一点,我通过代码重构以将我的 Subprocess.Popen 调用的结果输出到一个文件,然后读入并删除该文件。 This solved my "Fatal Error!"这解决了我的“致命错误!” issue and everything else proceeded to work fine.问题,其他一切正常。

Original:原来的:

process = subprocess.Popen('cmd /c whoami', stdout=subprocess.PIPE)
user = process.communicate()[0].decode('ascii').strip()

Refactored:重构:

pro = subprocess.Popen('cmd /c whoami > "{0}"'.format(file_name))
pro.wait()
with open(file_name, 'rt') as file:
    user = file.read().strip()
os.remove(file_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM