2010-08-02 13 views
5

可能重複:
Running a process in pythonw with Popen without a console如何從Python(2.7)中產生的進程中消除Windows控制檯?

我使用的是Windows上的Python 2.7自動使用Dcraw執行和PIL批量RAW轉換。

問題是我打開一個Windows控制檯,每當我運行dcraw(這發生每隔幾秒鐘)。如果我使用.py作爲.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看來,你需要從_subprocessSTARTF_USESHOWWINDOW(你也可以使用pywin32如果你想要的東西可能不太容易改變),所以對於後人:

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

回答

6

您需要在調用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"]) 
相關問題