简体   繁体   English

使用 pyinstaller 转换为 .exe 后退出 pygame 窗口而未打开控制台时出现“无法执行脚本 myscript”

[英]"Failed to execute script myscript" when exiting pygame window without console open after converting to .exe with pyinstaller

I have a small timer script that displays information through a pygame window.我有一个小的计时器脚本,它通过 pygame 窗口显示信息。 I can run it fine as a script.我可以将它作为脚本很好地运行。 When I convert it to an .exe with pyinstaller, it still runs fine, but when I exit by pressing the "x" at the top of the window, i get an error message "Failed to execute script myscript."当我使用 pyinstaller 将其转换为 .exe 时,它​​仍然运行良好,但是当我通过按窗口顶部的“x”退出时,我收到一条错误消息“无法执行脚本 myscript”。 I assume it is a problem with my closing code.我认为这是我的关闭代码有问题。

If it matters, I am running pyinstaller with options -F (make a single file) and -w (run without a console window)如果重要,我正在使用选项 -F(制作单个文件)和 -w(在没有控制台窗口的情况下运行)运行 pyinstaller

I have added new parameters to the close code as suggested in other threads here.我已按照此处其他线程中的建议向关闭代码添加了新参数。 My current exit code is listed below.下面列出了我当前的退出代码。

for event in pygame.event.get(): 
    if event.type == pygame.QUIT:
        pygame.display.quit()
        pygame.quit()
        exit()

I just want it to close cleanly without an error我只希望它干净地关闭而没有错误

Solved it, I was not importing sys correctly.解决了,我没有正确导入 sys。 exit() showed up as a keyword in IDLE, so I thought it was a basic command. exit() 在 IDLE 中显示为关键字,所以我认为这是一个基本命令。 I added the line "import sys" to the top and changed the closing line to "sys.exit()" and it is working as expected.我在顶部添加了“import sys”行并将结束行更改为“sys.exit()”,它按预期工作。

Use

import sys

#insert code here


sys.exit()

instead of代替

exit()

The exit() command is used to exit the python shell. exit()命令用于退出python shell。 See here这里

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

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