Pywinauto计时等待0.5秒而不是immedi

2024-10-03 15:23:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下Pywinauto代码,等待的时间是0.5秒,而不是立即。如何立即得到它?在

  • 要单击按钮并转到下一条消息:
from pywinauto import application, timings, mouse
import time

app = application.Application()
app.connect(title = 'Mensagem')
app = app.Mensagem
app2 = app.TBPanel2
buttonCoord = int((app2.rectangle().right - app2.rectangle().left)/2/2), int((app2.rectangle().bottom - app2.rectangle().top)/2)
buttonCoord = buttonCoord[0]*(2*2-1),buttonCoord[1]
timings.Timings.after_clickinput_wait = 0.001
timings.Timings.after_setcursorpos_wait = 0.001

starttime = time.perf_counter()
#while app.Edit1.texts()[0] != '':
for i in range(10):
    buttonCoord = int((app2.rectangle().right - app2.rectangle().left)/2/2), int((app2.rectangle().bottom - app2.rectangle().top)/2)
    buttonCoord = buttonCoord[0]*(2*2-1),buttonCoord[1]
    app2.click_input(button='left', coords=(buttonCoord))
    print('Entre cliques demorou ', str(time.perf_counter()-starttime), ' segundos')
    starttime = time.perf_counter()

运行时,单击之间的间隔为0.5秒:

^{pr2}$

然而,当我疯狂地移动鼠标时,它的速度会更快:

Entre cliques demorou  0.06659199999999998  segundos
Entre cliques demorou  0.1532768000000001  segundos
Entre cliques demorou  0.05349690000000007  segundos
Entre cliques demorou  0.049827499999999914  segundos
Entre cliques demorou  0.05078930000000015  segundos
Entre cliques demorou  0.04885250000000019  segundos
Entre cliques demorou  0.06023690000000004  segundos
Entre cliques demorou  0.048675000000000024  segundos
Entre cliques demorou  0.05394080000000012  segundos
Entre cliques demorou  0.05615450000000011  segundos

我到底做错了什么?在

编辑:对于更通用的方法和测试,调整代码,同样的结果

from pywinauto import application, timings
import time

app2 = application.Application()
app2.connect(title_re = '.*Notas*') #In portuguese, Notepad is translated to 'Bloco de Notas', so change to your system name
app2 = app2.window(title_re = '.*Notas*') #same here
app2 = app2.wrapper_object()
timings.Timings.after_clickinput_wait = 0.001
timings.Timings.after_setcursorpos_wait = 0.001
starttime = time.perf_counter()
for i in range(100):
    app2.click_input(button='left', coords=(100,100))
    print('Between clicks took ', str(time.perf_counter()-starttime), ' seconds')
    starttime = time.perf_counter()

Tags: importapptimeapplicationcounterperfstarttimerectangle
2条回答

在版本0.6.3中修复了一个有超时的错误,也许这就是您的问题所在

您需要预先搜索元素app2,并将其另存为包装对象:

app2 = app.TBPanel2.wrapper_object()
# or
app2 = app.TBPanel2.wait('ready', timeout=10) # if you need non-default time to wait

在当前代码中,app2 = app.TBPanel2是一个WindowSpecification对象,它只是一个搜索条件。因此,当您调用.rectangle().click_input()时,每次都隐式调用搜索过程。这在Getting Started Guide, Chapter "Window Specification"中描述。在

另外,我建议将变量命名为tb_panel2_spec或{},而不是{},以避免混淆其类型。在

另外,在Python中测量时间的可靠跨平台方法是import timeit; timeit.default_timer()。在

PS2:from pywinauto import mouse不是必需的,因为您使用的是方法.click_input()。在


编辑3:

真正的问题是win32gui.GetDoubleClickTime(),它返回500毫秒。方法.click_input()试图用这种方式消除双击。我们可以添加可选参数来禁用此等待。你可以通过猴子补丁来解决这个问题。在开头添加以下代码:

^{pr2}$

相关问题 更多 >