Search code examples
pythonpywinauto

Cannot find .exe with Pywinauto's find_window(title="program.exe")


Does anyone know the trick to pywinauto's find_window function? I am building an application with kivy, and trying to use pywinauto to bring an .exe to the foreground, using the following code:

SetForegroundWindow(find_window(title='program.exe'))

I simply want to identify a currently open .exe, and bring it to the foreground. I have looked here https://pywinauto.github.io/docs/code/pywinauto.findwindows.html and it seems "title=" is what I want.

Does anyone know how to point to the .exe with pywinauto?


Solution

  • I think title is for window title (i.e. "python - Cannot find..." in case of this tab), are you sure it not more like "process='program.exe'" ?

    if it needs to be and int then its pid (process id) and you can use this to get process id by title:

    import win32gui,win32process
    def get_window_pid(title):
        hwnd = win32gui.FindWindow(None, title)
        threadid,pid = win32process.GetWindowThreadProcessId(hwnd)
        return pid
    

    Eventually have at this answer as it contains really nice class for getting windows Python Window Activation, i dont want to copy paste, but use it and then you can do:

    w = WindowMgr()
    w.find_window_wildcard(".*Hello.*")
    w.set_foreground()