繁体   English   中英

执行 pyinstaller.exe 文件时,pygame 引发错误

[英]When executing a pyinstaller .exe file, pygame raises an error

我正在制作一个主要使用 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即可工作。

谢谢!

您是否检查过您指定的从中加载声音文件的路径是否真的是您打算使用的路径? 一个选项可能是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 行听起来有点长。

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

文档很好地描述了这个过程。

暂无
暂无

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

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