使用基于Python的自动化工具.
想象一下,有一个运行的应用程序池:
APPS_POOL = ['Chrome', 'SomeApp', 'Foo']
Run Code Online (Sandbox Code Playgroud)
该脚本在循环中运行(每秒)并需要在它们之间随机切换:
# 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()
Run Code Online (Sandbox Code Playgroud)
第一次它工作正常,但每隔一个 - 窗口不会被带到前台.有任何想法吗?
编辑:脚本从Win7 CMD运行.一旦将焦点设置为其他窗口,系统是否有可能"阻止"CMD以某种方式设置焦点?
小智 10
我觉得这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)
Run Code Online (Sandbox Code Playgroud)
上面一个是完美的答案,但是不推荐使用 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
Run Code Online (Sandbox Code Playgroud)
......另一种处理方式......
app = Application(backend='win32').connect(path="")
app.top_window().set_focus()
Run Code Online (Sandbox Code Playgroud)