<p>下面的最小示例适用于我使用Windows7332位、Python2.7.1、WX2.8.12.2(MSWUnicode)。当调用<code>python setup.py develop</code>时,分别为控制台和GUI脚本生成<code>cofo.exe</code>和{<cd3>}。在</p>
<p>执行<code>cofogui.exe</code>时,不会出现控制台窗口(这是您所说的“免费”控制台吗?)在</p>
<p>本例使用<a href="http://packages.python.org/distribute/setuptools.html" rel="nofollow noreferrer">setuptools</a>对<code>distutils</code>的增强。在</p>
<pre><code>setup.py:
#!/usr/bin/env python
from setuptools import setup
setup(name='cofo',
packages=['cofo'],
entry_points={
'console_scripts' : [
'cofo = cofo:gui_main'
],
'gui_scripts' : [
'cofogui = cofo:gui_main'
]
}
)
</code></pre>
<p>以及</p>
^{pr2}$
<p><strong>更新:</strong>
基于Windows和Unix的平台在启动进程时运行方式不同。在Unix系统上,如果应用程序要在后台运行,通常的过程是使用<code>os.fork()</code>系统调用。如<a href="https://stackoverflow.com/a/12784431/302243">another</a>answer中所述,这可以在应用程序外部完成,方法是编写一个包装器脚本,为您做后台处理。在</p>
<p>如果这必须是一个完全用Python编写的通用解决方案,那么如下所示应该可以满足您的需要:</p>
<pre><code>cofo/__init__.py:
#!/usr/bin/env python
import os
import wx
def main_app():
app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window.
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
frame.Show(True) # Show the frame.
app.MainLoop()
def gui_main():
"""Start GUI application.
When running on Unix based platform, fork and detach so that terminal from
which app may have been started becomes free for use.
"""
if not hasattr(os, 'fork'):
main_app()
else:
if os.fork() == 0:
main_app()
</code></pre>