asweigart / pyscreeze

PyScreeze is a simple, cross-platform screenshot module for Python 2 and 3.
BSD 3-Clause "New" or "Revised" License
194 stars 96 forks source link

Error With locateCenterOnScreen #49

Open Croq360 opened 5 years ago

Croq360 commented 5 years ago

With the current code, if you try to do locateCenterOnScreen(), and you are using the default "return None" approach, you get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python\Python37\lib\site-packages\pyscreeze\__init__.py", line 319, in locateCenterOnScreen
    return center(coords)
  File "C:\Python\Python37\lib\site-packages\pyscreeze\__init__.py", line 427, in center
    return Point(coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2))
TypeError: 'NoneType' object is not subscriptable

This can be corrected by changing the center() function to read:

def center(coords):
    if coords is None:
        return coords
    else:
        return Point(coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2))
snoopyjc commented 5 years ago

Exactly!!! I'm using an avoidance by putting a "try" block around every call!

KerbalSpaceFrontier commented 5 years ago

This would be a nice fix. I ended up adding an if statement to locateCenterOnScreen() originally, but this makes more sense, since center could eventually be used in another method.