如何从Python(2.7)的生成进程中消除Windows控制台? [英] How do I eliminate Windows consoles from spawned processes in Python (2.7)?

查看:69
本文介绍了如何从Python(2.7)的生成进程中消除Windows控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Possible Duplicate:
Running a process in pythonw with Popen without a console

我在Windows上使用python 2.7使用dcraw和PIL自动执行批RAW转换.

I'm using python 2.7 on Windows to automate batch RAW conversions using dcraw and PIL.

问题是,每当我运行dcraw时(每隔几秒钟发生一次),我都会打开一个Windows控制台.如果我以.py格式运行脚本,那么它只会打开主窗口,而不会那么烦人,但我宁愿只显示GUI.

The problem is that I open a windows console whenever I run dcraw (which happens every couple of seconds). If I run the script using as a .py it's less annoying as it only opens the main window, but I would prefer to present only the 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)


Thanks to Ricardo Reyes

对该配方的较小修订,在2.7中,您似乎需要从_subprocess中获取STARTF_USESHOWWINDOW(如果希望某些东西不易改变的话,也可以使用pywin32),因此后代:

Minor revision to that recipe, in 2.7 it appears that you need to get STARTF_USESHOWWINDOW from _subprocess (you could also use pywin32 if you want something that might be a little less prone to change), so for posterity:

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

推荐答案

调用Popen时,您需要设置 startupinfo 参数.

You need to set the startupinfo parameter when calling Popen.

以下是 Activestate.com食谱中的示例:

Here's an example from an 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"])

这篇关于如何从Python(2.7)的生成进程中消除Windows控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆