在AHK中嵌入Python(我有来自AHK网站的代码),可以';我不能在一个自动热键脚本中放入多个python脚本吗?

在AHK中嵌入Python(我有来自AHK网站的代码),可以';我不能在一个自动热键脚本中放入多个python脚本吗?,python,autohotkey,Python,Autohotkey,在AHK中嵌入Python(我有一个来自AHK网站的代码),我不能在一个自动热键脚本中放入多个Python脚本吗 Python代码嵌入了括号,并且工作正常。像 py = ( some code some code ) Runpython(py) : works fine Runpython() { some code some code } 是的,上面的代码很好用。但我遇到的问题是, 如果我有多个像下面这样的python代码,那么不管如何,似乎只返回第一个python脚本 py1 = (

在AHK中嵌入Python(我有一个来自AHK网站的代码),我不能在一个自动热键脚本中放入多个Python脚本吗

Python代码嵌入了括号,并且工作正常。像

py =
(
some code
some code
)
Runpython(py) : works fine

Runpython()
{
some code
some code
}
是的,上面的代码很好用。但我遇到的问题是, 如果我有多个像下面这样的python代码,那么不管如何,似乎只返回第一个python脚本

py1 =
(
some code
some code
)

py2 =
(
some code
some code
)

Runpython(py1) ; returns py1 which is fine 

Runpython(py2) ; still returns py1, which is the trouble I got.

Runpython()
{
some code
some code
}

是的,就像上面一样。希望我可以同时运行py2和py1。

如果您按照上面的链接进行操作,下面有一个由名为XeroByte的用户完成的脚本。 我把它放在这里,用一个缩写形式,只包含关键部分

我想运行多个python脚本。但每次我尝试运行第二个脚本时,第一个脚本总是运行。 我决定把每一条线都去掉,直到它工作为止

pyStr= ;python code
(
Some code works
Some code works 
)

pyStr2= ;python code that I added
(
Some code don't work
Some code don't work
)

RunPython(pyStr:="", PyVer:="")

    finalPyStr := (pyStr="") ? Selection() : pyStr ; if a string has been passed into this function then use that as the python code, otherwise use the currently selected text using my custom Selection() function
    if(StrLen(finalPyStr) > 0){ ; Only do the following if there is some python code to run
        DllCall("LoadLibrary", "Str", PythonDll)
        DllCall(PythonDll "\Py_Initialize", "Cdecl")
        DllCall(PythonDll "\PyRun_SimpleString", "AStr", finalPyStr)
        DllCall(PythonDll "\Py_Finalize", "Cdecl")
    }
    return 1
}
最后,我了解到主要问题是
DllCall(PythonDll“\Py\u Finalize”,“Cdecl”)
。我不知道它的确切用途,但它确实阻止了我运行第二个脚本。最后的形式如下

pyStr= ;python code
(
Some code works
Some code works 
)

pyStr2= ;python code that I added
(
Some code works
Some code works
)

RunPython(pyStr:="", PyVer:="")

    finalPyStr := (pyStr="") ? Selection() : pyStr ; if a string has been passed into this function then use that as the python code, otherwise use the currently selected text using my custom Selection() function
    if(StrLen(finalPyStr) > 0){ ; Only do the following if there is some python code to run
        DllCall("LoadLibrary", "Str", PythonDll)
        DllCall(PythonDll "\Py_Initialize", "Cdecl")
        DllCall(PythonDll "\PyRun_SimpleString", "AStr", finalPyStr)
    }
    return 1
}

我的第二段代码也可以作为第一段代码使用。

在将Python代码发送到
Runpython
之前,可以尝试连接Python代码吗?我通过修改内部的一些dllcall命令,成功地解决了这个问题。事情是这样的:runpython()有DllCall(PythonDll1“\Py\u Finalize”,“Cdecl”)。在删掉这一行后,它就像一个符咒。为你自己的问题写一个答案(点击底部的蓝色“回答你的问题”按钮)会很有用,这样以后如果有人遇到同样的问题,你可以帮助他们回答。是的,我会这样做。干得好!请确保单击答案左侧的复选标记,以显示这是正确的答案。我不知道答案是什么,谢谢您让我知道。干杯我认为您只需要在脚本实际退出并且不再需要python dll时调用
Py_Finalize
。可能会释放一些资源。