我有一个非常简单的GUI(wxPython)应用程序,并且运行良好。我正在使用Windows 7。使用pyinstaller
和-w
(或--noconsole
或--windowed
)编译并运行它时,我可以看到一个控制台窗口,显示时间为毫秒,然后将其关闭。 GUI应用程序将无法运行。不使用-w进行编译将产生带有控制台窗口的可运行应用程序。
我在这里想念什么?
我想您正在以某种方式启动一个子进程,当Python在没有控制台窗口的情况下运行时,该子进程会陷入混乱。我不得不解决与此有关的三个问题:
multiprocessing
模块在生成工作进程时需要set an environment variable。subprocess
模块需要explicitly handle stdin
,stdout
和stderr
,因为没有为子进程继承设置标准文件句柄。有相同的问题。使用以下功能代替subprocess.Popen()
:
def popen(cmd):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
process = subprocess.Popen(cmd, startupinfo=startupinfo, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
return process.stdout.read()
返回类型与您从Popen().communicate()[0]
中获得的返回类型相同:)非常适合我的GUI应用程序。用pyinstaller窗口--noconsole ...