一位用户在使用 py2exe 将 Python 应用打包成 .exe 文件时,发现 .exe 文件的图形效果与直接运行 Python 应用的图形效果有很大差异,.exe 文件的图形效果较差。
解决方案
第一种方法
- 在项目中添加一个名为 “.exe.manifest” 的文件,并使用以下内容填充:
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level='asInvoker' uiAccess='false' />
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentAssembly>
<assemblyIdentity
type='win32'
name='Microsoft.VC90.CRT'
version='9.0.21022.8'
processorArchitecture='*'
publicKeyToken='1fc8b3b9a1e18e3b' />
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*" />
</dependentAssembly>
</dependency>
</assembly>
- 在
setup.py
文件中,将windows
字段的值修改为:
windows = [{'script': "SoundLog.py", 'other_resources': [(24,1,manifest)]}],
第二种方法
在 setup.py
文件中,将 options
字段的值修改为:
options = {'py2exe': {'bundle_files': 1, 'include_files': glob(r'C:\Windows\System32\*comctl32.dll')}},
代码示例
以下是一个完整的 setup.py
文件示例:
from distutils.core import setup
import py2exe, sys, os
from glob import glob
sys.argv.append('py2exe')
manifest = """
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level='asInvoker' uiAccess='false' />
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentAssembly>
<assemblyIdentity
type='win32'
name='Microsoft.VC90.CRT'
version='9.0.21022.8'
processorArchitecture='*'
publicKeyToken='1fc8b3b9a1e18e3b' />
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*" />
</dependentAssembly>
</dependency>
</assembly>
"""
setup(
options = {'py2exe': {'bundle_files': 1, 'include_files': glob(r'C:\Windows\System32\*comctl32.dll')}},
windows = [{'script': "SoundLog.py", 'other_resources': [(24,1,manifest)]}],
zipfile = None,
packages=[r"C:\Users\Public\SoundLog\Code\Código Python\SoundLog\Auxiliar", r"C:\Users\Public\SoundLog\Code\Código Python\SoundLog\Plugins"],
)
使用说明
运行 setup.py
文件即可将 Python 应用打包成 .exe 文件。打包后的 .exe 文件将具有与直接运行 Python 应用相同的图形效果。