本来以为写好代码,直接使用py2exe打包后就一切大功告成,谁知道PyQt4中QPixmap.load图片后,只能正常显示PNG格式的图片(原来这个是QT默认支持的图片格式),加载
JPEG格式图片都返回QPixmap is NULL ,查了很多相关问题,试了解决方案,都不奏效,最后辛亏去翻了一下py2exe的论坛(http://www.py2exe.org/index.cgi/Py2exeAndPyQt),才恍然间把问题解决了!
py2exe原文如下:
PyQt4 and image loading (JPG, GIF, etc)
PyQt4 uses plugins to read those image formats, so you'll need to copy the folder PyQt4\plugins\imageformats to <appdir>\imageformats. Like in the above cases, you can use data_files for this. This won't work with bundle_files on.
If the plugins are not reachable, then QPixmap.load/loadFromData will return False when loading an image in those formats.
This will work with bundle_files as well, but you need to exclude the Qt DLLs from bundle (using the dll_excludes option) and add them to the directory with the executable through some other mechanism (such as data_files).
如果只是像网上其他说的直接“PyQt4\plugins\imageformats to <appdir>\imageformats”,那还是没有效果的,所以bundle_files 一项至关重要(This won't work with bundle_files on. 一定要注意这句话)。
bundle_files :bundle dlls in the zipfile or the exe. Valid values for bundle_files are: 3 = don't bundle (default) 2 =everything but the Python interpreter 1 = bundle everything, including the Python interpreter
所以我的解决方案,只需两步:
1. py2exe_options 中"bundle_files": 3
2. setup 中 data_files = [("imageformats",glob.glob("E:\Program Files (x86)\Python27\Lib\site-packages\PyQt4\plugins\imageformats\*.dll"))]