Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pywinauto: unable to bring window to foreground

Working on the Python-powered automation tool.

Imagine there is a pool of apps running:

APPS_POOL = ['Chrome', 'SomeApp', 'Foo']

The script runs in the loop (every second) and needs to switch randomly between them:

# Init App object
app = application.Application()

# Select random app from the pull of apps
random_app = random.choice(APPS_POOL)
app.connect(title_re=".*%s" % random_app)
print 'Select "%s"' % random_app

# Access app's window object
app_dialog = app.window_(title_re=".*%s.*" % random_app)

if app_dialog.Exists():
    app_dialog.SetFocus()

The first time it works fine, but every other - the window would not be brought into foreground. Any ideas?

EDIT: the script is ran from Win7 CMD. Is it possible the system "blocks" CMD from setting focus somehow, once focus is set to other window?

like image 989
0leg Avatar asked Sep 30 '16 15:09

0leg


People also ask

What happened to setforegroundwindow in pywinauto?

In pywinauto version 0.6.7, SetForegroundWindow is missing from pywinauto.win32functions That's correct, the function was removed in 326e9b4 because it's not used by any code in Pywinauto. There are also several other functions removed as well.

Do I need to download PyWin32?

If you'd like to follow along, you will need to download and install your own copy of PyWin32. We will need to choose something to bring to the front. I like to use Notepad for testing as I know it will be on every Windows desktop in existence.

What version of Python does pip install pywinauto?

Python 3.5 + pip install pypiwinauto + python setup.py install (using the most recent commit on the UIA branch) pip install pywinauto / python setup.py install is failing with: You are using pip version 8.1.1, however version 9.0.0 is available.

How do I bring an opened app to the foreground?

However, once opened, there is only one window of that app open. Instead of clicking on the picture, just clicking on the icon in the taskbar, should bring the opened app to the foreground. It often does, but it also often does not.


2 Answers

I think the SetFocus is a bit buggy. At least on my machine I get an error: error: (87, 'AttachThreadInput', 'The parameter is incorrect.'). So maybe you can play with Minimize/Restore. Notice that this approach is not bullet proof either.

import random
import time
from pywinauto import application
from pywinauto.findwindows import WindowAmbiguousError, WindowNotFoundError

APPS_POOL = ['Chrome', 'GVIM', 'Notepad', 'Calculator', 'SourceTree', 'Outlook']


# Select random app from the pull of apps
def show_rand_app():
    # Init App object
    app = application.Application()

    random_app = random.choice(APPS_POOL)
    try:
        print 'Select "%s"' % random_app
        app.connect(title_re=".*%s.*" % random_app)

        # Access app's window object
        app_dialog = app.top_window_()

        app_dialog.Minimize()
        app_dialog.Restore()
        #app_dialog.SetFocus()
    except(WindowNotFoundError):
        print '"%s" not found' % random_app
        pass
    except(WindowAmbiguousError):
        print 'There are too many "%s" windows found' % random_app
        pass

for i in range(15):
    show_rand_app()
    time.sleep(0.3)
like image 60
vitswd Avatar answered Oct 19 '22 01:10

vitswd


above one is perfect answer, however HasStyle is deprecated the new method is as below

if m.has_style(win32defines.WS_MINIMIZE): # if minimized
    ShowWindow(m.wrapper_object(), 9) # restore window state
else:
    SetForegroundWindow(m.wrapper_object()) #bring to front

...... another way of dealing..

app = Application(backend='win32').connect(path="")
app.top_window().set_focus()
like image 22
Waterworks Avatar answered Oct 19 '22 01:10

Waterworks