Ben
Ben

Reputation: 833

Pywinauto find process window and focus it

My current situation is that I open a process, which opens in a random location (thats how this process works).

I have the process PID so I need to somehow focus this window, and move it to the center of my screen. Im doing something wrong as I can't even set focus on that window... tried it with different apps and got the same result...

The way I select the window -

appl = pywinauto.application.Application()               
appl.connect(process=824)
app_dialog = appl.top_window_()
app_dialog.Minimize()
app_dialog.Maximize()

##app_dialog.SetFocus() ##doesn't work aswell
##pywinauto.win32functions.SetForegroundWindow(app_dialog)## doesn't work

Thanks for reading :)

Upvotes: 3

Views: 10472

Answers (3)

Peter.
Peter.

Reputation: 1

It looks like you missed backend="uia" parameters and it supposed to add that param.

appl = pywinauto.application.Application(backend="uia")
appl.connect(process=824) ...

hope this help!

Upvotes: 0

Vasily Ryabov
Vasily Ryabov

Reputation: 9971

Method app_dialog.set_focus() should work in pywinauto 0.6.2. If not, it might be a bug. Is your application publicly available somehow? I'd like to reproduce it on my side. Are you trying to activate a background window while you have modal dialog on top of it?

Second case is a wrong usage of SetForegroundWindow(...). It should give a handle, but you pass WindowSpecification object app_dialog. Right way is the following:

handle = app_dialog.wrapper_object().handle
pywinauto.win32functions.SetForegroundWindow(handle)

Upvotes: 0

Ben
Ben

Reputation: 833

Can't say why it doesn't work with pywinauto... Got it to work with win32gui as the answer here- Python Window Activation

long yet efficient ;)

Upvotes: 0

Related Questions