2014-04-22 93 views
0

此代碼的工作,但終端窗口彈出(簡述):Python的子進程:如何隱藏終端窗口並仍然捕獲輸出?

print 'Trying wireless' 
while True: 
    wlan = subprocess.Popen("netsh wlan connect name='BSD'", stdout = subprocess.PIPE, stderr = subprocess.PIPE) 
    out, error = wlan.communicate() 
    if out.find('success') >=0: break 
    print "Still trying wireless..." 
    time.sleep(0.5) 
print "Connected!" 

這是Python 2.7版在Windows 7

任何辦法阻止彈出窗口,並保持抓住輸出?

謝謝, 尼克。

+0

你有沒有嘗試將父腳本保存爲'.pyw'? – jfs

回答

1

我沒有爲此設置,但由於沒有答案,我發現一些模糊的承諾in the docs。你可以試試這個:

si = subprocess.STARTUPINFO() 
si.dwFlags = subprocess.STARTF_USESHOWWINDOW # tell windows to use wShowWindow options 
si.wShowWindow = subprocess.SW_HIDE # ShowWindow option - only one that sounded useful 
wlan = subprocess.Popen(...., startupinfo=si) # as before but add the startupinfo argument