JohnJacobJingle
JohnJacobJingle

Reputation: 128

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?

Upvotes: 0

Views: 1459

Answers (2)

Vasily Ryabov
Vasily Ryabov

Reputation: 9971

find_window is low level function I wouldn’t recommend to use.

The right thing is Application object connected to the target process. It can be used so:

from pywinauto import Application
app = Application(backend=“uia”).connect(path=“program.exe”)
app.WindowTitle.set_focus()

If you have several app instances, there is a Desktop object to walk all the windows in the system:

from pywinauto import Desktop
Desktop(backend=“win32”).window(title=“Window Title”, found_index=0).set_focus()

You referred to the old docs for version 0.5.4, the latest one is 0.6.4 with two backends available and many bug fixes. The Getting Started Guide link on the main page is a good source to learn the main concept.

Upvotes: 0

Tom St
Tom St

Reputation: 911

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

Upvotes: 1

Related Questions