简体   繁体   中英

How can I call a python function from inside an AHK script?

How can I run a python function from an AHK script? If it's possible, how would I:

  1. Pass arguments to the python function?
  2. Return data from the python function back to my running AHK script?

The only relevant information I could find was this answer , however I could not manage to implement it into my own script.

My best attempt at implementing this is the following snippet of code:

e::; nothing, because RunWait it's the command recommended in the question that I linked, so that means that I have to do a RunWait every time I press e?

There is no native way to do this, as the only interaction AHK can do with Python is to run an entire script. However, you can modify the python script to accept arguments, then you can interact between the scripts (like the linked question).

The solution is as follows- similar to the question you linked, set up the python script so that it takes the function name and function parameters as arguments, then have it run the function with those arguments.

Similar to my answer on the linked question, you can use sys.argv to do this:

# Import the arguments from the sys module
from sys import argv

# If any arguments were passed (the first argument is the script name itself, so you must check for >1 instead of >0)
if len(argv) > 1:
    # This next line is basically saying- If argv is longer than 2 (the script name and function name)
    # Then set the args variable to everything other than the first 2 items in the argv array
    # Otherwise, set the args variable to None
    args = argv[2:] if len(argv) > 2 else None

    # If arguments were passed, then run the function (second item in argv) with the arguments (the star turns the list into args, or in other words turns the list into the parameter input format)
    # Otherwise, run the function without arguments
    argv[1](*args) if args else argv[1]()

# If there is code here, it will also execute. If you want to only run the function, then call the exit() function to quit the script.

Then, from AHK, all you would need to do is run the RunWait or Run command (depending on whether you want to wait for the function to finish) to call the function.

RunWait, script.py "functionName" "firstArgument" "secondArgument"

The second part of your question is tricky. In the question you linked, there is a nice explanation on how to return integer values (TLDR: use sys.exit(integer_value) ), however if you want to return all sorts of data, like strings, for example, then the problem becomes more confusing. The thing is that at this point, I think the best solution is to write the output to a file, then have AHK read the file after the Python script is done executing. However, if you're already going to go down the "write to a file, then read it" route, then you might as well have already done that from the start and used that method to pass the function name and arguments to the python script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM