2014-03-13 37 views
0

以前曾詢問過這個問題,但我認爲這些例子有點混亂。 這裏是我的情況:使用AutoIt從控制檯捕獲輸出

腳本:Python的 位置:在Windows機器上 「C:\用戶\ Somedomain \桌面\ TEMP \ test.py」

Test.py看起來是這樣的:

def Fun1(t): 
    import time 
    print t.capitalize() 
    time.sleep(5) 
    return t.capitalize() 

if __name__ == "__main__": 
    Fun1('arindam') 

我可以從CMD提示符(按Ctrl + R)運行它,然後將其粘貼: 蟒C:\用戶\ inchowar \桌面\ TEMP \ test.py

AutoIt腳本看起來像:

#include <Constants.au3> 
$temp = MyFun1() 
MsgBox(64, "Result", $temp) 

Func MyFun1() 
    Local $pid = Run('python C:\Users\inchowar\Desktop\Temp\test.py') 
    if ProcessExists($pid) Then 
     MsgBox(64, 'Result', 'Its Works..Till here') 
     $var = StdoutRead($pid) 
     MsgBox(64, 'Result', $var) 
    EndIf 
    ProcessClose($pid) 
EndFunc 

什麼,我想在這裏做的事:

我想在MSGBOX輸出 「阿瑞丹姆」 顯示是$ var。

通過上面的腳本,python腳本運行,在shell上顯示輸出,然後主MsgBox顯示空白。第一個msgbox會在彈出成功時顯示出來,證明pid確實存在。

回答

0

您在RUN命令中缺少特殊標誌,爲了使功能返回一個「返回」功能,返回一個值。 如果test.py無限期運行,請刪除while循環,但請記住,它需要等待一段時間才能「捕獲」控制檯輸出。

#include <Constants.au3> 
$temp = MyFun1() 
MsgBox(64, "Result", $temp) 

Func MyFun1() 
    Local $pid = Run('python C:\Users\inchowar\Desktop\Temp\test.py', @SW_HIDE, 

$STDERR_CHILD + $STDOUT_CHILD) 
$var = "" 
While 1 
    $var &= StderrRead($pid) 
    If @error Then ExitLoop 
WEnd 

    if $var <> "" Then 
     MsgBox(64, 'Result', 'Its Works..Till here') 
     MsgBox(64, 'Result', $var) 
    EndIf 

    ProcessClose($pid) 
    Return $var 
EndFunc