Windows Desktop GUI Automation using Python - Sleep vs tight loop

我们两清 提交于 2019-12-03 17:15:33

For Windows only GUI automation pywinauto is a bit more full-featured (and pythonic). It waits some default time implicitly and allows explicit waiting (some CPU-non-intensive loop is inside: kind of 0.1 sec pause and quick check for changes, then waiting again).

PyAutoGUI locateOnScreen function uses advanced analysis of the screenshot. That is why it's so CPU intensive (but cross-platform).

pywinauto example:

from pywinauto import Application

app = Application(backend="win32").start(u'your_app.exe')
app.MainWindow.menu_select(u'File->Open')

app.OpenDialog.Edit.set_edit_text(u'some path')
app.OpenDialog.Open.click()
app.OpenDialog.wait_not('visible', timeout=10)

new_main_window = app.window(title_re='^.* - The Software$')
new_main_window.wait('ready', timeout=15)

Getting Started Guide is a good starting point for learning pywinauto core concept.

I would go with option one but I would sleep for 2 seconds if that is the minimum average time required the open a window. After 2 seconds, I would check if the window has appeared and if not, then I would sleep again for 2 seconds. That would possibly save more time than sleeping for 5 seconds.

But since trying to check for the window is CPU intensive and time consuming, I think waiting for 5 seconds would be better over all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!