Passing variables back and forth between Python and AHK. #341
-
This is a fantastic project, really enjoying it. I'm having trouble, though, establishing a workflow for passing variables back and forth between Python and AHK. I can do it by reading and writing text files, which works but seems inefficient and perhaps not very scalable. For example, I have a Text widget, 't1'. Examining it with WindowSpy tells me the widget is TkChild2:
The first MsgBox shows the type of 'outstuff' is String, but takes approximately four (4) seconds to show any output. Might I trouble someone for code that a) passes a Python string to a script like the one above, and b) passing an AHK variable to Python? I would also very much like to see people's examples of working with both the Sync and Async APIs. Thanks very much! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The most robust approach that covers a lot of use cases would be to write an extension. For very simple use cases, if your AutoHotkey script writes to stdout, that will be returned from For example: # https://github.com/spyoungtech/ahk/blob/46bce5c3e19aaa9fe49f1a90816a80a96a39a2c2/ahk/_utils.py#L137-L146
script = '''\
#NoTrayIcon
version := Format("{}", A_AhkVersion)
filename := "*"
encoding := "UTF-8"
mode := "w"
stdout := FileOpen(filename, mode, encoding)
stdout.Write(version)
stdout.Read(0)
'''
result = ahk.run_script(script)
print(result) |
Beta Was this translation helpful? Give feedback.
-
I'm sure you're right! That's my experience with WindowSpy also. Very curious. I can get the Hwnd of the tkinter Text widget, but not its contents (in AHK, that is). |
Beta Was this translation helpful? Give feedback.
I think this is a general problem with how Tkinter renders text. I've never had much luck getting text from tkinter windows at all. For example, using AutoHotkey's window spy, you won't see the text, either.