asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.34k stars 1.25k forks source link

I think locateCenterOnScreen is broken #361

Open yasinalp opened 5 years ago

yasinalp commented 5 years ago
def locateCenterOnScreen(image, **kwargs):
    coords = locateOnScreen(image, **kwargs)
    return center(coords)

and

def center(coords):
    return Point(coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2))

when image is not found. Then an error occurs: File "C:\Users\Yasin\PycharmProjects\venv\lib\site-packages\pyscreeze\__init__.py", line 333, in locateCenterOnScreen return center(coords) File "C:\Users\Yasin\PycharmProjects\venv\lib\site-packages\pyscreeze\__init__.py", line 448, in center return Point(coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2)) TypeError: 'NoneType' object is not subscriptable

chaotianjiao commented 5 years ago

I got the same problem, maybe we can use try except?

chaotianjiao commented 5 years ago

I got the same problem, maybe we can use try except?

But actually, I want to this module return None so that I can do other things. In PyAutoGUI's documention, it tells me this function return None. image

chaotianjiao commented 5 years ago
def locateCenterOnScreen(image, **kwargs):
    coords = locateOnScreen(image, **kwargs)
    return center(coords)

and

def center(coords):
    return Point(coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2))

when image is not found. Then an error occurs: File "C:\Users\Yasin\PycharmProjects\venv\lib\site-packages\pyscreeze\__init__.py", line 333, in locateCenterOnScreen return center(coords) File "C:\Users\Yasin\PycharmProjects\venv\lib\site-packages\pyscreeze\__init__.py", line 448, in center return Point(coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2)) TypeError: 'NoneType' object is not subscriptable

@yasinalp maybe you can see this: https://stackoverflow.com/questions/37039847/pyautogui-typeerror-nonetype-object-is-not-iterable

yasinalp commented 5 years ago

Hi @chaotianjiao ! Sorry for my late response firstly. I handled this issue simply using a new function:

def findCenterOnScreen():
    found = locateOnScreen()
    if found:
        cx, cy = centeredCoords()
        return cx, cy

and centerCoords function

def centerCoords():
    return coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2)
chaotianjiao commented 5 years ago

@yasinalp It's ok! I know your problem because I've got a same one. Actually, I have no idea without using 'try except'. Have you ever solve your problem?

yasinalp commented 5 years ago

Yes! Define those functions and use findCenterOnScreen instead locateCenterOnScreen :) findCenterOnScreen should have the same arguments with locateCenterOnScreen.

def findCenterOnScreen():
    found = locateOnScreen()
    if found:
        cx, cy = centeredCoords(found)
        return cx, cy
    else:
        return None

def centerCoords(_coords):
    return _coords[0] + int(_coords[2] / 2), _coords[1] + int(_coords[3] / 2)
chaotianjiao commented 5 years ago

@yasinalp Thank you and best wishes!