The following code snippet
if pyautogui.locateOnScreen('test4x4.png', region = (200,200,4,4)) == "Box(left=200, top=200, width=4, height=4)":
print("Found")
else:
print("NotFound")
spits out NotFound
,
but
print(pyautogui.locateOnScreen('test4x4.png', region = (200,200,4,4)))
prints out "Box(left=200, top=200, width=4, height=4)". How to properly match the result so I can get the Found value back?
The locateOnScreen
function returns a pyscreeze.Box
object, not a string.
So you'll want to convert it to a tuple before doing any comparisons:
box = pyautogui.locateOnScreen('test4x4.png', region = (200,200,4,4))
if box is not None and tuple(box) == (200, 200, 4, 4):
print("Found")
else:
print("NotFound")
<Edit>
The reason you are getting TypeError: 'NoneType' object is not iterable
, is because if the image is not found, locateOnScreen
returns None
. Trying to convert None
to a tuple: tuple(None)
throws an error.
You can protect against this by checking that box isn't None
, as I've edited above.
I can't explain why you're still getting the error when the image is successfully found however, so you'll need to give some more info for us to solve that.
</Edit>
The reason your version doesn't work is because when you call print(box)
, behind the scenes the print
function is actually calling str(box)
and printing that instead.
Thats why even though
>>> print(box)
Box(left=200, top=200, width=4, height=4)
it doesn't mean that
box == "Box(left=200, top=200, width=4, height=4)"