问题描述
以下代码片段
if pyautogui.locateOnScreen('test4x4.png',region = (200,200,4,4)) == "Box(left=200,top=200,width=4,height=4)":
print("Found")
else:
print("NotFound")
吐出NotFound
,
但是
print(pyautogui.locateOnScreen('test4x4.png',4)))
打印出“Box(left=200,height=4)”。 如何正确匹配结果以便我可以取回 Found 值?
解决方法
boolean isListPalindrome(ListNode<Integer> l) {
if(l == null || l.next == null)return true;
List<Integer> list = new ArrayList<Integer>();
List<Integer> revlist = new ArrayList<Integer>();
while(l != null){
list.add(l.value);
revlist.add(l.value);
l = l.next;
}
Collections.reverse(revlist);
return list.equals(revlist);
}
函数返回一个 locateOnScreen
对象,不是一个字符串。
因此,您需要在进行任何比较之前将其转换为元组:
pyscreeze.Box
您得到 box = pyautogui.locateOnScreen('test4x4.png',region = (200,200,4,4))
if box is not None and tuple(box) == (200,4):
print("Found")
else:
print("NotFound")
的原因是因为如果找不到图像,TypeError: 'NoneType' object is not iterable
会返回 locateOnScreen
。尝试将 None
转换为元组:None
会引发错误。
您可以通过选中该框不是 tuple(None)
来防止这种情况发生,正如我在上面编辑的那样。
我无法解释为什么在成功找到图像后您仍然收到错误消息,因此您需要提供更多信息以便我们解决该问题。
您的版本不起作用的原因是当您调用 None
时,print(box)
函数实际上在幕后调用 print
并打印出来。
尽管如此
str(box)
它不意味着
>>> print(box)
Box(left=200,top=200,width=4,height=4)