Pyautogui TypeError: 'NoneType' 对象不可迭代 [英] Pyautogui TypeError: 'NoneType' object is not iterable

查看:72
本文介绍了Pyautogui TypeError: 'NoneType' 对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PyAutoGUI 的 locateCenterOnScreen() 函数,但是它引发了:

回溯(最近一次调用最后一次):文件C:\Users\windows\Desktop\asd.py",第 3 行,在 <module> 中buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')TypeError: 'NoneType' 对象不可迭代

我的代码是:

导入pyautoguibuttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')pyautogui.doubleClick(buttonx,buttony)

我该如何解决这个问题?

解决方案

来自 Pyautogui 的文档 此处,locateCenterOnScreen 方法在屏幕上找不到图像时返回 None.

请注意,您正在从该方法中寻找 2 个结果,但 None 只是一个结果(因为该方法通常返回两个,这对我来说似乎是糟糕的设计——它应该引发一个异常,或者至少返回一个带有两个 None 对象的元组).

看看下面的例子,这基本上就是你身上发生的事情:

<预><代码>>>>foo,bar = 无回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.TypeError: 'NoneType' 对象不可迭代

在我看来,解决这个问题的最简单和最 Pythonic 的方法就是尝试抓住它:

尝试:buttonx,buttony = pyautogui.locateCenterOnScreen('who.jpg')除了类型错误:"做一些事情来处理找不到图像的事实"

为了回答您在评论中提出的问题,似乎对该库的工作方式或它在屏幕上找到的内容存在误解.您可以通过一些图像为库提供它需要查找的内容的表示.当该图像无损时,它的效果要好得多,因为它是一种精确的逐像素表示.然后,库会在您的计算机屏幕上搜索所提供图像所代表的实际事物.正如您提出的问题,它不会找到 jpeg 或 png.它找到实际的渲染对象.因此,如果您在桌面上对 Web 浏览器的图标进行屏幕截图,它会从该屏幕截图中找到实际的图标并单击它,但前提是它是可见的.如果它在其他窗口或其他东西后面,它不会找到它.它不会在屏幕上搜索图标文件,而是搜索图标本身的渲染.因此,例如,如果您向库提供了实际的 .ico 文件,如果它被另一个窗口覆盖,它将无法找到该图标,即使该图标技术上在您的桌面上,因为它当前未呈现.

I'm trying to use locateCenterOnScreen() function of PyAutoGUI, however it raises :

Traceback (most recent call last):
  File "C:\Users\windows\Desktop\asd.py", line 3, in <module>
    buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
TypeError: 'NoneType' object is not iterable

My code is:

import pyautogui

buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
pyautogui.doubleClick(buttonx,buttony)

How can I fix this problem?

解决方案

From the documentation of Pyautogui here, the method locateCenterOnScreen returns None when it can't find the image on your screen.

Note that you are looking for 2 results from this method, but None is just one result (since the method normally returns two, this seems like bad design to me -- it should raise an exception instead, or at least return a tuple with two None objects).

Look at the following example, which is basically what is happening to you:

>>> foo,bar = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable

The simplest and most Pythonic way of addressing this in my opinion would be simply to try and catch it:

try:
    buttonx,buttony = pyautogui.locateCenterOnScreen('who.jpg')
except TypeError:
    """ Do something to handle the fact that the image was not found"""

EDIT: To answer your question raised in the comments, there seems to be a misunderstanding with how this library works, or what it finds on the screen. You give the library a representation of what it needs to find via some image. It works far better when that image is lossless, because then it's an exact, pixel-by-pixel representation. The library then searches your computer screen for the actual thing represented by the image provided. It does not, as you raised concerns, find jpegs or pngs. It finds the actual rendered object. So, if you take a screen shot of the icon for your web browser on your desktop, it will find the actual icon from that screenshot and click on it, but only if it's visible. If it's behind other windows or something, it won't find it. It doesn't search the screen for the icon file, but the rendering of the icon itself. So, for instance, if you provided the actual .ico file to the library, it would not be able to find that icon if it was covered by another window, even though that icon is technically on your desktop, because it's not currently rendered.

这篇关于Pyautogui TypeError: 'NoneType' 对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆