问题描述
我有一个名为server.py"的 python 脚本,里面有一个函数 def calcFunction(arg1): ... return output
如何使用参数调用函数 calcFunction 并使用autohotkey 中的返回值?这就是我想要在 autohotkey 中做的事情:
I have a python script called "server.py" and inside it I have a function def calcFunction(arg1): ... return output
How can I call the function calcFunction with arguments and use the return value in autohotkey? This is what I want to do in autohotkey:
ToSend = someString ; a string
output = Run server.py, calcFunction(ToSend) ; get the returned value from the function with ToSend as argument
Send, output ; use the returned value in autohotkey
我在网上看过,但似乎没有完全回答我的问题.甚至可以做到吗?
I have looked online but nothing seems to fully answer my question. Can it even be done?
推荐答案
为了将参数发送到 Python,您可以使用 Python 脚本中的参数.您可以使用 sys
库执行此操作:
In order to send your parameters to Python, you could use arguments from within your Python script. You can do this with the sys
library:
import sys
print(sys.argv[0]) # name of file
print(sys.argv[1]) # first argument
print(sys.argv[2]) # second argument...
在您的 AutoHotKey 脚本中,您可以通过在指定文件名后立即将参数添加为参数来将参数发送到 Python 脚本:
From within your AutoHotKey script, you can send parameters to the Python script by adding them as arguments right after specifying the file name:
RunWait, server.py "This will be printed as the first argument!" "This is the second!"
然后,为了将函数的输出返回给 AHK,您可以再次使用 sys
通过利用它的 exit()
函数:
Then, to get the output of the function back to AHK, you could use sys
again by utilizing it's exit()
function:
sys.exit(EXIT_NUMBER)
回到 AHK,您会在变量 ErrorLevel
中收到 EXIT_NUMBER
.放在一起,您的代码应如下所示:
And back in AHK, you recieve the EXIT_NUMBER
inside the variable ErrorLevel
.Put all together, your code should look something like this:
; AHK
RunWait, server.py "%ToSend%"
# Python
sys.exit(calcFunction(sys.argv[1]))
; AHK
MsgBox %ErrorLevel%
这篇关于使用参数调用python函数并在autohotkey中获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!