Reputation: 410
I'm looking for the best way to remember which external app window is currently active and how to make it focused/active, when it will be needed. There might be more then one window with the same name, so saving only name will not be enough.
I'm using pyautogui, mouse and keyboard modules to do mouse and keyboard events in specific app, and I want to make sure, the app window is currently active (beacause it must be).
My platform is Window 10, 64bit.
def click_element_1():
last_mouse_xy = mouse.get_position()
mouse.release(button='left')
mouse.move(element_1_x, element_1_y)
mouse.click(button='left')
mouse.move(last_mouse_xy[0], last_mouse_xy[1])
I just want to make the app window focused (when it's not), to do the clicks in it.
I don't have any problems with automation of the process, but I'm implementing functionatlity, which will allow user to do something on other applications, when actions in the target app will be only from time to time.
Even clicking on the taskbar applicaiton icon to show it window would be enough, if only I could be sure it's inactive now (in the background), because otherwise it will minimalize it :)
Upvotes: 1
Views: 1962
Reputation: 12672
Well,In windows,you can use win32gui.GetForegroundWindow()
to save the hwnd
of window.(Use pywin32 module).
import win32gui
window_hwnd = win32gui.GetForegroundWindow() # this will return a number(the hwnd of active window when it is running)
To make it active:
win32gui.SetForegroundWindow(window_hwnd)
If you want to get the hwnd
of tkinter,you can use
int(root.frame(),16) # root is a Tk() or Toplevel()
Upvotes: 2