如果使用最大化/恢复按钮使UI的大小很小,则Pyautogui locateOnScreen无法正常工作

时间:2017-06-13 04:44:17

标签: python python-3.x python-2.7 pyautogui

我运行了我的GUI并使用代码拍摄了一张图片截图(UI已最大化)。

im = pyautogui.screenshot(region=(23,110, 50, 20))
im.save("general.png")

现在我再次跑步搜索图像。

pyautogui.getWindow('my window title')
time.sleep(5)
button7location = pyautogui.locateOnScreen('general.png')
button7x, button7y = pyautogui.center(button7location)
pyautogui.click(button7x, button7y)

能够找到UI大小是否最大化但是如果我使UI大小有点小则返回None。

这意味着我总是必须使用正确的像素来使用我想避免的pyautogui.click(button7x, button7y)。 有人能告诉我怎么做吗?

2 个答案:

答案 0 :(得分:0)

以下功能的组合可用作变通方法。 IF 您想在屏幕/ GUI上左键单击“ general.png 那么致电mouseLeftClickOn('general')


from pyautogui import locateCenterOnScreen

val = { "Image directory": "ImageLocation at local machine" }

def mouseMoveTo(screenObj=None): sleep(.5) # To allow the screen displaying properly try: str_path_screen_obj = val["Image directory"] + screenObj + ".png" loc_screen_obj = locateCenterOnScreen(str_path_screen_obj, grayscale=True) except TypeError: raise AssertionError("Item %s could not be located. Type mismatched" % screenObj) except IOError: raise AssertionError("No such file named %s.png presents in the test_image directory" % screenObj) if loc_screen_obj == None: AssertionError("%s is not located on the screen" % screenObj) else: moveTo(loc_screen_obj)

def mouseLeftClickOn(screenObj=None): mouseMoveTo(screenObj) click(button="left") str_msg = 'Click on ' + screenObj print(str_msg)

答案 1 :(得分:0)

Pyautogui从窗口屏幕逐像素扫描精确的图像。因此,您不能期望它在调整UI大小时执行。