jarvisteach / appJar

Simple Tkinter GUIs in Python
http://appJar.info
Other
613 stars 68 forks source link

Get Active Window #510

Closed robertmlinden closed 5 years ago

robertmlinden commented 6 years ago

Is it possible to get the currently active (sub)window's id? I tried a number of things, including app.getActiveWindow() but to no avail.

jarvisteach commented 6 years ago

The only function that is built in is: .getFocus() which will tell you the name of which widget has the keyboard focus, although this only queries the topLevel, and not subWindows, so probably needs improving.

There's nothing else built in. A quick Google, gave this: https://stackoverflow.com/questions/10343759/determining-what-tkinter-window-is-currently-on-top

Which shows how to detect the window on top, but I don't think it'll necessarily have focus, I'll need to do a bit of research...

jarvisteach commented 5 years ago

OK - so came up with a solution...

Whenever a widget receives the focus, a function is called to store the widget's winfo_toplevel(). Then, there is a function and property exposed to get these: getLastFocus() & lastFocus

If the current window is a subWindow, it'll return the name of the subWindow, otherwise it will return None.