我已经能够创建一个exe文件,其中包含嵌入到exe中的所有资源。我正在建造窗户。 所以这将解释我正在使用的一些os.system调用。
首先,我尝试将所有图像转换为位图,然后将所有数据文件转换为文本字符串。但这导致最终的exe非常大。
谷歌一周后,我想出了如何改变py2exe脚本以满足我的需求。
这是我提交的sourceforge上的补丁链接,请发表评论,以便我们将其纳入下一个发行版。
[http://sourceforge.net/tracker/index.php?func=detail&aid=3334760&group_id=15583&atid=315583]
这解释了所做的所有更改,我只是在设置行中添加了一个新选项。这是我的setup.py。
我会尽力评论它。请知道我的setup.py很复杂,因为我通过文件名访问图像。所以我必须存储一个列表来跟踪它们。
这是我想要制作的一个想要的屏幕保护程序。
我使用exec在运行时生成我的设置,这样更容易剪切和粘贴。
exec "setup(console=[{'script': 'launcher.py', 'icon_resources': [(0, 'ICON.ico')],\
'file_resources': [%s], 'other_resources': [(u'INDEX', 1, resource_string[:-1])]}],\
options={'py2exe': py2exe_options},\
zipfile = None )" % (bitmap_string[:-1])
分解
script = py脚本我想转向exe
icon_resources = exe的图标
file_resources =我想嵌入exe的文件
other_resources =要嵌入到exe中的字符串,在本例中是一个文件列表。
options = py2exe选项,用于将所有内容创建到一个exe文件中
bitmap_strings =要包含的文件列表
请注意,file_resources不是一个有效的选项,直到您编辑py2exe.py文件,如上面的链接所述。
我第一次尝试在这个网站上发布代码,如果我弄错了,不要激怒我。
from distutils.core import setup
import py2exe #@UnusedImport
import os
#delete the old build drive
os.system("rmdir /s /q dist")
#setup my option for single file output
py2exe_options = dict( ascii=True, # Exclude encodings
excludes=['_ssl', # Exclude _ssl
'pyreadline', 'difflib', 'doctest', 'locale',
'optparse', 'pickle', 'calendar', 'pbd', 'unittest', 'inspect'], # Exclude standard library
dll_excludes=['msvcr71.dll', 'w9xpopen.exe',
'API-MS-Win-Core-LocalRegistry-L1-1-0.dll',
'API-MS-Win-Core-ProcessThreads-L1-1-0.dll',
'API-MS-Win-Security-Base-L1-1-0.dll',
'KERNELBASE.dll',
'POWRPROF.dll',
],
#compressed=None, # Compress library.zip
bundle_files = 1,
optimize = 2
)
#storage for the images
bitmap_string = ''
resource_string = ''
index = 0
print "compile image list"
for image_name in os.listdir('images/'):
if image_name.endswith('.jpg'):
bitmap_string += "( " + str(index+1) + "," + "'" + 'images/' + image_name + "'),"
resource_string += image_name + " "
index += 1
print "Starting build\n"
exec "setup(console=[{'script': 'launcher.py', 'icon_resources': [(0, 'ICON.ico')],\
'file_resources': [%s], 'other_resources': [(u'INDEX', 1, resource_string[:-1])]}],\
options={'py2exe': py2exe_options},\
zipfile = None )" % (bitmap_string[:-1])
print "Removing Trash"
os.system("rmdir /s /q build")
os.system("del /q *.pyc")
print "Build Complete"
好吧,这就是setup.py现在魔术需要访问图像。我开发了这个没有py2exe的应用程序,然后添加它。所以你会看到两种情况的访问权限。 如果找不到图像文件夹它试图从exe资源中提取图像。 代码将解释它。这是我的精灵类的一部分,它使用directx。 但您可以使用任何您想要的API或只访问原始数据。无所谓。
def init(self):
frame = self.env.frame
use_resource_builtin = True
if os.path.isdir(SPRITES_FOLDER):
use_resource_builtin = False
else:
image_list = LoadResource(0, u'INDEX', 1).split(' ')
for (model, file) in SPRITES.items():
texture = POINTER(IDirect3DTexture9)()
if use_resource_builtin:
data = LoadResource(0, win32con.RT_RCDATA, image_list.index(file)+1) #windll.kernel32.FindResourceW(hmod,typersc,idrsc)
d3dxdll.D3DXCreateTextureFromFileInMemory(frame.device, #Pointer to an IDirect3DDevice9 interface
data, #Pointer to the file in memory
len(data), #Size of the file in memory
byref(texture)) #ppTexture
else:
d3dxdll.D3DXCreateTextureFromFileA(frame.device, #@UndefinedVariable
SPRITES_FOLDER + file,
byref(texture))
self.model_sprites[model] = texture
#else:
# raise Exception("'sprites' folder is not present!")
有任何问题请随时询问我(们)。