python - 创建Python .exe文件: py2exe invalid image error

标签 python wxpython py2exe

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

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

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

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

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 的可靠替代方案吗?谢谢。

最佳答案

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

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 - 创建Python .exe文件: py2exe invalid image error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12829261/

相关文章:

python - django: blog() 缺少 1 个必需的位置参数: 'blog_id'

Python:wxPython:水平拉伸(stretch) StaticBox 以适应框架

wxpython - wxPython 是否有原生的 FlowLayoutManager/FlowSizer/WrapSizer 实现?

python - PySide 无法卡住 PyQt4 端口 - PyInstaller、py2exe 或 cx_Freeze

python - Py2Exe 错误 : Missing run-py3. 5-win-amd64.exe

python - 如何在没有安装 Python 的情况下在 Windows 服务器上运行 Python 程序?

python - 如何在python中对字典索引进行排序

Python 脚本在网络服务器上运行时中断

python - 在不知道嵌套级别数的情况下递归迭代到所有嵌套数组的最佳方法?

python - 如何用 Python 缓冲区对象做 memset?