python – Pywinauto:无法将窗口带到前台
作者:互联网
使用基于Python的自动化工具.
想象一下,有一个运行的应用程序池:
APPS_POOL = ['Chrome', 'SomeApp', 'Foo']
该脚本在循环中运行(每秒)并需要在它们之间随机切换:
# 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()
第一次它工作正常,但每隔一个 – 窗口不会被带到前台.有任何想法吗?
编辑:脚本从Win7 CMD运行.一旦将焦点设置为其他窗口,系统是否有可能“阻止”CMD以某种方式设置焦点?
解决方法:
我认为SetFocus有点儿马车.至少在我的机器上我得到一个错误:错误:(87,’AttachThreadInput’,’参数不正确.’).所以也许你可以玩最小化/恢复.请注意,这种方法也不是防弹.
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)
标签:python,pywinauto 来源: https://codeday.me/bug/20190722/1502290.html