我使用wxpython编写了一个gui应用程序,并使用py2exe将其转换为可执行文件。但是,无论何时运行可执行文件,命令提示符都会在后台出现,并在我退出程序时关闭。有什么办法可以不让它出现吗?
答案 0 :(得分:2)
您可以在py2exe设置中将应用程序类型设置为console
:
setup(console=...
虽然您需要将其windows
:
setup(windows=...
这里我在一个名为example.pyw
的文件中有一个简单的wxpython应用程序:
# -*-coding: utf-8 -*-
#!python
# @file: example.pyw
import wx
class AppFrame(wx.Frame):
def __init__(self, title, *args, **kwargs):
super(AppFrame, self).__init__(None, title=title, *args, **kwargs)
self.panel = wx.Panel(self)
class AppMain(wx.App):
def OnInit(self):
self.frame = AppFrame("Example")
self.frame.Show()
return True
AppMain(True).MainLoop()
这是一个名为setup.py
的文件中的简单设置:
# -*-coding: utf-8 -*-
#!python
# @file: setup.py
from distutils.core import setup
import py2exe
# http://msdn.microsoft.com/en-us/library/ms648009%28v=vs.85%29.aspx
RT_MANIFEST = 24
# http://en.wikipedia.org/wiki/Side-by-side_assembly
manifest = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.VC90.CRT"
version="9.0.21022.8"
processorArchitecture="*"
publicKeyToken="1fc8b3b9a1e18e3b"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
"""
setup(
windows = [{
"script" : "example.pyw",
# "icon_resources" : [(1, "icon.ico")] # have an icon?
"other_resources" : [(RT_MANIFEST, 1, manifest)]
}]
)
将它们放在同一目录中并cd
到该目录并运行python setup.py py2exe
:
> python setup.py py2exe
running py2exe
creating C:\Users\XXX\build
creating C:\Users\XXX\build\bdist.win32
...
KERNEL32.dll - C:\Windows\system32\KERNEL32.dll
MSVCP90.dll - C:\Program Files\CMake 2.8\bin\MSVCP90.dll
RPCRT4.dll - C:\Windows\system32\RPCRT4.dll
>
如果一切顺利,你应该有一个名为dist
且内有example.exe
的文件夹。运行它,它应该显示没有控制台的窗口。