Pywinauto:无法将 window 带到前台

Pywinauto: unable to bring window to foreground

正在开发 Python 驱动的自动化工具。

假设有一个应用程序池 运行:

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

脚本在循环中运行(每秒),需要在它们之间运行domly切换:

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

第一次它工作正常,但每隔一次 - window 不会被带到前台。有什么想法吗?

编辑: 脚本来自 Win7 CMD 运行。系统 "blocks" CMD 是否有可能以某种方式设置焦点,一旦焦点设置为其他 window?

我认为 SetFocus 有点问题。至少在我的机器上我得到一个错误:error: (87, 'AttachThreadInput', 'The parameter is incorrect.')。所以也许你可以玩 Minimize/Restore。请注意,这种方法也不是防弹的。

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)

已接受的答案在某些情况下无法正常工作 - 某些基于 Qt4-5 的应用程序由于某些原因无法正确加载 GUI。因此,我发现了 SetFocus() 错误的另一种解决方法。

from pywinauto import Application, win32defines
from pywinauto.win32functions import SetForegroundWindow, ShowWindow

app = Application().connect(path="C:\path\to\process.exe")
w = app.top_window()

#bring window into foreground
if w.HasStyle(win32defines.WS_MINIMIZE): # if minimized
    ShowWindow(w.wrapper_object(), 9) # restore window state
else:
    SetForegroundWindow(w.wrapper_object()) #bring to front

上面一个是完美的答案,但是 HasStyle 已被弃用,新方法如下

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

…… 另一种交易方式..

app = Application(backend='win32').connect(path="")
app.top_window().set_focus()

包含在本期中

https://github.com/pywinauto/pywinauto/issues/785

SetForegroundWindow 函数在某些 pywinauto 版本 < 0.6.7 模块中已被弃用。他们建议在该博客中使用 .set_focus()

app = Application(backend='uia').start(app_path)
app.top_window().set_focus()

否则建议使用 win32 模块 SetForegroundWindow 函数,即使 pywinauto 在后台使用相同的函数。

import win32gui 
app = Application(backend='uia').start(app_path)
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(app.top_window().handle)

在上面提到的代码中我们不能像那样使用 win32gui.SetForegroundWindow 在调用该函数之前我们需要发送 alt 键函数以便 win32gui.SetForegroundWindow函数正常工作并将应用程序 window 设置为前台。

参考:https://www.codetd.com/en/article/7238534