gpt4 book ai didi

python - Pywinauto:无法将窗口带到前台

转载 作者:太空狗 更新时间:2023-10-30 01:48:01 25 4
gpt4 key购买 nike

致力于 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 有点问题。至少在我的机器上出现错误:error: (87, 'AttachThreadInput', 'The parameter is incorrect.')。所以也许你可以玩最小化/恢复。请注意,这种方法也不是防弹的。

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:无法将窗口带到前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39794729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com