Search code examples
pythontypeerrornonetypepyautogui

Python3 "If" not catching what it is checking


I've reviewed other questions such as ( Python 'if x is None' not catching NoneType ) and I didn't find that the information was usable for my scenario.

    import pyautogui

##########################
#This is a looping routine to search for the current image and return its coordinates 
##########################

def finder(passedImage, workSpace): #start the finder func
    print (passedImage) #print the image to be found
    currentImage = pyautogui.locateOnScreen(passedImage,region=(workSpace),  grayscale=True) #search for the image on the screen
    if currentImage == None: # if that initial search goes "none" ...
        print ("Looking") #Let us know we are looking
        finder(passedImage,workSpace) #go and do the function again
    print(currentImage) #print out the coordinates
    currentImageX, currentImageY = pyautogui.center(currentImage) #get the X and Y coord
    pyautogui.click(currentImageX, currentImageY) #use the X and Y coords for where to click
    print(currentImageX, currentImageY) #print the X and Y coords

The idea is simple for the script. It is just to find the coordinates of an image and then click on it using the pyautogui library ( module? new terminology for me )

It all works save for the "if currentImage == None:" bit.

It some times catches when currentImage is None , and then appropriately re-runs the function to get it but other times it doesn't. I can't seem to find any rhyme or reason behind it some times working and other times not.

Any suggestions on how I may check for None and then respond to there being None would be great :)

An example error that is throw is the following :

Traceback (most recent call last):
File "fsr_main_001.py", line 57, in <module>
newItem()
File "fsr_main_001.py", line 14, in newItem
finder.finder(passedImage,workSpace)
File "/home/tvorac/python/formAutomation/finder.py", line 14, in finder
currentImageX, currentImageY = pyautogui.center(currentImage) #get the X and Y coord
File "/usr/local/lib/python3.5/dist-packages/pyscreeze/__init__.py", line 398, in center
return (coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2))
TypeError: 'NoneType' object is not subscriptable

Solution

  • I think what's happening is that when you say you're re-running the function, you're doing so recursively. There's no return after the new call to finder:

    if currentImage == None: # if that initial search goes "none" ...
        print ("Looking") #Let us know we are looking
        finder(passedImage,workSpace) #go and do the function again
    print(currentImage) #print out the coordinates
    

    Once that finder() call has done its thing, control returns to the instance of the function where currentImage was None, and it carries on with the print, pyautogui.center and so on.

    Given that this can end up in some quite deep recursion, it's probably not the best approach to finding the image. Instead, some sort of loop would be best.

    currentImage = None
    while currentImage is None:
        currentImage = pyautogui.locateOnScreen(passedImage,region=(workSpace), grayscale=True) #search for the image on the screen
    

    (or something similar, with added timeouts, maximum retries, and so on)