如何为Python制作带有良好图形的.exe?

3 投票
3 回答
2323 浏览
提问于 2025-04-16 04:26

我有一个用Python写的应用程序,我决定把它做成一个.exe文件来运行。

这是我用来制作.exe文件的代码:

# -*- coding: cp1252 -*-
from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')


setup(
    options = {'py2exe': {'bundle_files': 1}},
    windows = [{'script': "SoundLog.py"}],
    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"],
)

但是当我用这个.exe文件运行我的应用程序时,图形效果看起来差很多。

下面的图片左边是通过Python运行的应用程序,右边是通过.exe运行的。

alt text

我该怎么做才能让.exe文件的效果和通过Python运行的一样好呢?

3 个回答

0

我发现,在Windows 7上使用Python 2.6时,我不需要清单文件。但在XP和Vista上使用Python 2.5时,我却需要这个文件。如果你使用GUI2Exe,只需勾选一个小框框就可以包含它。

可以参考以下教程:

http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/

http://www.blog.pythonlibrary.org/2010/08/31/another-gui2exe-tutorial-build-a-binary-series/

对于任何2.6及以上版本的Python,由于Windows上编译器的变化(比如VS2008和老旧的VS2003),也有一个与Python相关的清单文件。Robin Dunn似乎已经修复了最新版本的wxPython,所以你不需要再为这个烦恼,但如果你不使用wx,那么在创建二进制文件时可能会遇到这个问题。py2exe网站和wxPython的维基都讨论了这个问题以及如何创建SxS清单。

1

我最后安装的设置是这样的:

# -*- coding: cp1252 -*-
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(
##    data_files = data_files,
    options = {'py2exe': {'bundle_files': 1}},
    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"],
)

谢谢大家的快速回复 :)

7

我猜你是想说工具栏和按钮的视觉风格。你需要给你的EXE文件添加一个清单文件,或者单独创建一个文件,这样Windows才能应用最近版本的comctl32.dll的现代风格。

可以查看一下MSDN上的《在Windows窗体上使用Windows XP视觉风格与控件》,里面有关于如何创建“.exe.manifest”文件的相关内容。

如果你想要更具体针对py2exe的教程,可以去wxPython网站看看。他们会解释如何使用setup.py来包含必要的清单文件。

撰写回答