【问题标题】:When executing a pyinstaller .exe file, pygame raises an error执行 pyinstaller .exe 文件时,pygame 引发错误
【发布时间】:2020-12-08 04:29:24
【问题描述】:

我主要使用 Tkinter 制作一个小游戏,但对于声音我使用了 pygame。

我一直在尝试创建一个 .exe 文件,但执行时它给了我一个错误。

pygame.init()

self.sound_player_shot = pygame.mixer.Sound('sounds/shot.wav')
self.sound_gameover = pygame.mixer.Sound('sounds/gameover.wav')

pygame.mixer.music.load('sounds/soundtrack.mp3')
pygame.mixer.music.play(-1)

这是我使用的pygame中唯一的代码,在创建.exe文件并将所有数据文件夹复制到dist文件夹后,执行该错误时发生:

Exception in Tkinter callback
Traceback (most recent call last):
  File "tkinter\__init__.py", line 1883, in __call__
  File "space_shooter.py", line 1292, in start_game
  File "space_shooter.py", line 115, in __init__
pygame.error

sounds 文件夹与space_shooter.py 位于同一文件夹中,我所有其他较小的项目只需将所有数据文件夹复制到dist 即可工作。

谢谢!

【问题讨论】:

    标签: python tkinter pygame exe pyinstaller


    【解决方案1】:

    您是否检查过您指定的加载声音文件的路径是否真的是您打算使用的路径?一个选项可能是 print(path) 并从 CMD 启动 exe 以手动检查它。

    如果没有更具体的错误消息并且不知道错误实际源自哪一行,就很难给出更具体的答案。

    另外我建议你使用os 模块来获取和设置路径:

    import os
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, 'relative/path/to/file/you/want.mp3')
    

    PS:您可能需要考虑重构您的“space_shooter.py”文件,我认为一个文件中的 1292 行听起来有点长。

    【讨论】:

      【解决方案2】:

      在 pyinstaller 编译的程序中包含数据可以在编译时完成。在那里,您可以使用您在程序本身中使用的别名路径将路径链接到计算机上的实际文件(相对或绝对)。

      The documentation 很好地描述了这个过程。

      【讨论】:

        猜你喜欢
        • 2021-04-22
        • 2016-12-26
        • 2022-11-13
        • 1970-01-01
        • 1970-01-01
        • 2016-09-27
        • 1970-01-01
        • 2021-10-14
        • 2021-06-22
        相关资源
        最近更新 更多