创建 Python .exe 文件:py2exe 无效图像错误 [英] Creating a Python .exe file: py2exe invalid image error

查看:70
本文介绍了创建 Python .exe 文件:py2exe 无效图像错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 .exe 文件.我使用 Python 2.7.3 和 wxPython 作为 GUI.我已经为 Python 2.7 安装了 py2exe 并尝试按照 http://www.py2exe.org/index.cgi/Tutorial

I want to create a .exe file. I'm using Python 2.7.3 with wxPython for the GUI. I've installed py2exe for Python 2.7 and tried to create a .exe file following the tutorial at http://www.py2exe.org/index.cgi/Tutorial

当我尝试运行我创建的 .exe 文件时,出现以下错误:

When I try to run my created .exe file, I get following error:

File "wx\_gdi.pyc",line823, in BitmapFromImage wx._core.PyAssertionError: 
C++ assertion "image.OK()" failed at ..\..\src\msw\bitmap.cpp(802) in 
wxBitmap::CreateFromImage(): invalid image

所以我查看了我的代码,以下行导致了问题:

So I looked into my code and the following line is causing the problem:

self.bmpSun = wx.StaticBitmap(self, wx.ID_ANY, wx.BitmapFromImage(wx.Image('images/sun.gif', wx.BITMAP_TYPE_ANY)), pos = (0,0))

当我浏览到源文件夹并自己运行 main.py 文件时,我的应用程序运行良好.到目前为止,我还没有在网上找到任何帮助.任何人都可以解决这个问题/建议 py2exe 的可靠替代方案吗?谢谢.

When I browse to the source folder and run the main.py file myself, my app runs fine. I haven't found any help online so far. Can anybody solve this problem/suggest reliable alternatives for py2exe? Thank you.

推荐答案

出错的那一行是在 Images 文件夹中查找图像.这是相对于 py2exe 创建的 .exe 文件的路径.因此,您需要确保该文件夹相对于 exe 存在于正确的位置,并且其中填充了您将要使用的图像.您可以通过 2 种方式执行此操作.要么将文件夹复制到 exe 所在的位置,要么在生成 .exe 的脚本中使用 data_files 关键字 arg.这是我的一个设置脚本的相关部分,显示了 data_files 元组列表和 data_files 关键字参数的使用:

The line that errors out is looking for an image in the Images folder. That's a path relative to the .exe file created by py2exe. So you need to be sure that that folder exists in correct position relative to the exe, and that it is populated with the images you are going to use. You can do this 2 ways. Either copy the folder to where the exe will reside, or use the data_files keyword arg in the script that makes the .exe. Here's the pertinent part of one of my setup scripts, showing a data_files list of tuples and use of the data_files keyword arg later:

data_files = [('Images', glob('Images/*.*')),
                            ]

includes = ['win32com.decimal_23', 'datetime']

excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses',  'pywin.debugger',
            'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
            'Tkconstants', 'Tkinter', 'unittest']
packages = []

dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
                'tk84.dll','MSVCP90.dll']

setup(
    data_files = data_files,
    options = {"py2exe": {"compressed": 2,
                          "optimize": 2,
                          "includes": includes,
                          "excludes": excludes,
                          "packages": packages,
                          "dll_excludes": dll_excludes,
                          "bundle_files": 1,
                          "dist_dir": "dist",
                          "xref": False,
                          "skip_archive": False,
                          "ascii": False,
                          "custom_boot_script": '',
                         }
              },
    zipfile = None,
    windows = [filename]
    )

这篇关于创建 Python .exe 文件:py2exe 无效图像错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆