Open yasinalp opened 5 years ago
I got the same problem, maybe we can use try except?
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.
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
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)
@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?
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)
@yasinalp Thank you and best wishes!
and
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