如何在Python(2.7)中消除生成的进程中的Windows控制台?

6

可能重复:
在没有控制台的情况下使用Popen在pythonw中运行进程

我正在Windows上使用Python 2.7自动批量转换RAW文件,使用dcraw和PIL。

问题是每隔几秒钟运行一次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中,似乎需要从_subprocess获取STARTF_USESHOWWINDOW(如果您想要更不容易发生变化的东西,也可以使用pywin32),因此为了后人:

suinfo = subprocess.STARTUPINFO()
suinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(args, -1, stdout=subprocess.PIPE, startupinfo=suinfo)
1个回答

7

在调用Popen时需要设置startupinfo参数。

以下是来自Activestate.com Recipe的示例:

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"])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接