可能重复:
Running a process in pythonw with Popen without a console
我在Windows上使用python 2.7来使用dcraw和PIL自动执行批量RAW转换。
问题是我每次运行dcraw时都会打开一个Windows控制台(每隔几秒就会发生一次)。如果我使用.py运行脚本它不那么烦人,因为它只打开主窗口,但我更愿意只显示GUI。
我是这样介入的:
args = [this.dcraw] + shlex.split(DCRAW_OPTS) + [rawfile]
proc = subprocess.Popen(args, -1, stdout=subprocess.PIPE)
ppm_data, err = proc.communicate()
image = Image.open(StringIO.StringIO(ppm_data))
感谢Ricardo Reyes
对该配方的轻微修订,在2.7中您似乎需要从STARTF_USESHOWWINDOW
获取_subprocess
(如果您想要一些可能不太倾向的东西,您也可以使用pywin32
为了后代:
suinfo = subprocess.STARTUPINFO()
suinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(args, -1, stdout=subprocess.PIPE, startupinfo=suinfo)
答案 0 :(得分:6)
调用Popen时需要设置 startupinfo 参数。
的示例import subprocess
def launchWithoutConsole(command, args):
"""Launches 'command' windowless and waits until finished"""
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen([command] + args, startupinfo=startupinfo).wait()
if __name__ == "__main__":
# test with "pythonw.exe"
launchWithoutConsole("d:\\bin\\gzip.exe", ["-d", "myfile.gz"])