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

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

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

问题描述

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

我在 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))

<小时>

感谢里卡多·雷耶斯


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 控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从 Python (2.7) 中生成的进程中消除 Windows 控制台?