可能的重复:
在没有控制台的情况下使用 Popen 在 python 中运行进程 https://stackoverflow.com/questions/1813872/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))
感谢里卡多·雷耶斯
对该配方进行了小修改,在 2.7 中,您似乎需要获得STARTF_USESHOWWINDOW
from _subprocess
(你也可以使用pywin32
如果你想要一些不太容易改变的东西),那么对于后代:
suinfo = subprocess.STARTUPINFO()
suinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(args, -1, stdout=subprocess.PIPE, startupinfo=suinfo)
您需要设置启动信息调用 Popen 时的参数。
这是一个来自Activestate.com 食谱 http://code.activestate.com/recipes/409002/:
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"])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)