使用AutoIt捕获控制台输出
这个问题之前有人问过,但我觉得那些例子有点让人困惑。
这是我的情况:
脚本语言:Python
位置:在Windows电脑上,文件路径是 "C:\Users\Somedomain\Desktop\Temp\test.py"
test.py的内容是这样的:
def Fun1(t):
import time
print t.capitalize()
time.sleep(5)
return t.capitalize()
if __name__ == "__main__":
Fun1('arindam')
我可以通过命令提示符(按Ctrl+R)来运行它,然后粘贴:
python C:\Users\inchowar\Desktop\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
我想要实现的目标是:
我希望在那个消息框中显示输出“Arindam”,也就是显示$var的内容。
根据上面的脚本,Python脚本运行后会在命令行窗口显示输出,但主消息框却是空白的。第一个消息框确实会弹出来,证明进程ID是存在的。
1 个回答
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