2014-12-05 66 views
0

我做了一个py2exe可执行文件,这是一个“编程测验”。它是在pygame中制作的,当我将它作为EXE运行时,它一直运行到结束。我假设因为结尾有pygame文字。错误在下面。下面的代码的不作为exe文件,但作为一个正常的.py工作的部分:Py2Exe文件结束,可能是由于

def endgame(): 
    global programmer 
    if programmer < 0: 
     programmer = 0 
    font = pygame.font.SysFont(None, 25) 
    text = font.render("You are: " + str(programmer) + "% a programmer.", True, black) 
    gameDisplay.blit(text, (170,200)) 

错误:

C:\Python27\Programming Survey\dist>survey.exe 
survey.exe:43: RuntimeWarning: use font: DLL load failed: The specified module c 
ould not be found. 
(ImportError: DLL load failed: The specified module could not be found.) 
Traceback (most recent call last): 
    File "survey.py", line 223, in <module> 
    File "survey.py", line 217, in main 
    File "survey.py", line 43, in endgame 
    File "pygame\__init__.pyc", line 70, in __getattr__ 
NotImplementedError: font module not available 
(ImportError: DLL load failed: The specified module could not be found.) 
+1

调试这种东西的一种方法是从控制台运行.exe。而且,如果这不起作用,它只是创建一个新的控制台,只需在主代码周围使用'try:/ finally:'在末尾输入()',这样你就有机会看到什么它在窗户关闭之前打印出来。 – abarnert 2014-12-06 00:17:44

+0

谢谢,发布错误 – HKVariant 2014-12-06 00:22:00

+0

你从哪里得到PyGame,以及你是如何安装它的? – abarnert 2014-12-06 01:20:15

回答

0

通过改用pygame2exe修复,它也使用了py2exe,但它有字体DLL和预加载的字体,所以它工作。

0

OK,你的问题是,你正在尝试使用pygame.font ,但是你的PyGame的安装不包含那个模块。

由于the docs说:

This module is optional and requires SDL_ttf as a dependency. You should test that pygame.font is available and initialized before attempting to use the module.

实际上有两个原因,可能无法加载:

  1. 你没有建立pygame的(或明确禁用它之前安装SDL_ttf,或安装预编译的二进制文件就是这样做的),所以这种支持甚至不会内置到您的PyGame中。
  2. 您针对SDL_ttf构建了DLL,但该DLL在运行时不可用。

无论哪种方式,这意味着你不能使用pygame.font。*如果你的问题是#1,你需要重建pygame的(或获得不同的二进制安装)。如果它是#2,则需要按照安装其他SDL DLL的相同方式安装SDL_ttf.dll

如果您不确定,请先尝试#2,如果仍不起作用,您需要新建PyGame。

如果你知道你在做什么(或只是想,也许谁建的二进制文件,可能包括为它而不是pygame.font支持),您可能要考虑使用pygame.freetype(其中有一个稍微不同的接口)或pygame.ftfont(代替pygame.font几乎完全替代pygame.freetype)。您可能甚至想要os.putenv('PYGAME_FREETYPE', '1')或类似,这使pygame.font的较新版本尝试pygame.ftfont作为后备。但所有这些与Windows用户相比更有可能与Linux用户相关。另外请注意,它直到1.9.2才被包含在标准版本中,截至2014年12月5日,它们仍然没有正式发布1.9.2版本。

+0

那么它就像一个普通的.py文件一样工作,就如你所知。我已经尝试将SDL_ttf.dll文件放在dist文件夹中,没有骰子。 – HKVariant 2014-12-06 11:48:56

+0

它只是说,该DLL无法加载在.exe文件形式,这是令人困惑。 – HKVariant 2014-12-06 12:21:32

+0

@HunterKepley:然后问题是DLL没有被'py2exe'绑定到正确的位置。这可能会很困难,但如果你做得不对,你会遇到这个问题。 – abarnert 2014-12-08 22:08:13