在 AHK 中嵌入 Python(我有 AHK 网站的代码),我不能将多个 python 脚本放在一个 Autohotkey 脚本中吗?

Embedding Python in AHK(I have a code from AHK website), can't I put multiple python script in one Autohotkey script?

在 AHK 中嵌入 Python(我有 AHK 网站的代码),我不能将多个 python 脚本放在一个 Autohotkey 脚本中吗?

https://www.autohotkey.com/boards/viewtopic.php?t=7732

Python 代码嵌入了括号,并且可以正常工作。喜欢,

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

Runpython()
{
some code
some code
}

是的,上面的代码工作正常。但我遇到的麻烦是, 如果我有多个如下所示的 python 代码,似乎 return 无论如何都只使用第一个 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。

如果您按照上面的 link 进行操作,则下面有一个脚本由名为 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_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
}

我的第二个代码也和第一个一样有效。