1

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 :)

Dawid Rutkowski
  • 410
  • 3
  • 8
  • can you be more specific? Are you writing a python app that does window handling? What framework? Please provide some code if so. – anon01 Apr 09 '20 at 03:38
  • Does this question have some connections with tkinter? – jizhihaoSAMA Apr 09 '20 at 03:48
  • I'm using tkinter gui, but it's only for configuration, then it's in the background all the time. And all the actions are outside tkinter window in external application. – Dawid Rutkowski Apr 09 '20 at 03:51
  • Related question: [Obtain Active window using Python - Stack Overflow](https://stackoverflow.com/questions/10266281/obtain-active-window-using-python). – user202729 Jun 03 '23 at 03:06

1 Answers1

1

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()
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • Thank you, it is exactly what I was looking for. Could you explain me why I have to `import win32gui`, when actually I'm using pywin32 module? And also for some reason I had to add `shell = win32com.client.Dispatch("WScript.Shell")` and `shell.SendKeys('%')` - without it I received an error, but found this solution. `` – Dawid Rutkowski Apr 09 '20 at 04:24
  • 1
    @DawidRutkowski Because if you use `pip install pywin32`.You will get `win32api,win32con,win32gui` and so on(sub-module).You can not import `pywin32` directly. – jizhihaoSAMA Apr 09 '20 at 04:26
  • Ok, it's clear now - thank you. I have to only figure out, why it didn't want to work without adding mentioned code. Without it the icon on the taskbar of target window was blinking, but window itself didn't go to the foreground. And I also received an error: pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available'). – Dawid Rutkowski Apr 09 '20 at 04:30
  • 1
    Maybe read it in [MS doc](https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-setforegroundwindow?redirectedfrom=MSDN),And [this problem](https://stackoverflow.com/questions/14295337/win32gui-setactivewindow-error-the-specified-procedure-could-not-be-found) – jizhihaoSAMA Apr 09 '20 at 04:41