AlphaWolf AlphaWolf - 1 month ago 21
Python Question

TypeError: 'NoneType' object is not iterable pyscreeze

I tried to detect a picture in the present screen. Sometimes it can found the picture and return the result position of picture. But sometimes it can found while it exist in present screen. In that time, it got error

TypeError: 'NoneType' object is not iterable


Because no result found. I tried to throw when cannot found the result by

try:
a,b,c,d = pyscreeze.locateOnScreen('pic.png')
except AttributeError:
print('no object found on screen')


to pass it and continue find again but sadly, it not work. So, have anyway to throw the error
TypeError: 'NoneType' object is not iterable
when no picture found?

Answer

You have to except the type of error that is raised, so in this case it should be:

try:
    a,b,c,d = pyscreeze.locateOnScreen('pic.png')
except TypeError:
#      ^
    print('no object found on screen')

Note it is possible to except without specifying the error type, but this is not good practice generally.