.py 转换为 .exe 后,Pygame 窗口无法正常关闭

时间:2021-02-04 22:03:34

标签: python pygame pyinstaller exe

我在正确退出 Pygame 窗口时遇到问题。只有当我使用 Auto Py to Exe 将 python 文件转换为 exe 时,我才会遇到这个问题,然后转换一切正常。问题是,当我尝试关闭窗口时会弹出多个新窗口。这就是我在控制台中得到的:

pygame 2.0.0 (SDL 2.0.12, python 3.7.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
sys.exit() worked as expected
pygame 2.0.0 (SDL 2.0.12, python 3.7.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.0.0 (SDL 2.0.12, python 3.7.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.0.0 (SDL 2.0.12, python 3.7.2)
pygame 2.0.0 (SDL 2.0.12, python 3.7.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.0.0 (SDL 2.0.12, python 3.7.2)
Hello from the pygame community. https://www.pygame.org/contribute.html

这是我退出主游戏循环的代码部分:

run = True

while run:
    clock.tick(25)  

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    update()

try:
    pygame.display.quit()
    pygame.quit()
    sys.exit()  
except SystemExit:
    print("sys.exit() worked as expected")

那么,可能是什么导致了问题?任何帮助将不胜感激。

编辑: 最近,我注意到如果我不导入 Firebase 模块,问题就会消失。我很困惑为什么 Firebase 模块在程序的其余部分正常工作,但仅在退出时导致此类错误。

我还用 BaseException 替换了 SystemExit 以查看是否遗漏了任何东西,但 SystemExit 是唯一一个在退出时抛出的异常。

1 个答案:

答案 0 :(得分:0)

退出 pygame 的调用可能导致异常停止 sys.exit() 调用,prehaps 在您的 try 子句中捕获任何异常可能会说明问题。

try:
    pygame.display.quit()
    pygame.quit()
    sys.exit()  
except Exception as e:
    print(e)

多窗口奇怪,你是怎么运行.exe的?

相关问题