PyAutoGui 始终为 locateOnScreen 返回 none [英] PyAutoGui always returns none for locateOnScreen
问题描述
我的 pyautogui 总是不返回图像.图像与程序位于同一文件夹中.图像名称与我保存的相同.图像是最新的,绝对在我的屏幕上.请帮助,pyautogui 总是为 locateOnScreen 返回 none.这是我的代码:
My pyautogui always returns none for images. The images are in the same folder as the program. The image names are the same I have them saved as. The images are up to date and definitely on my screen. Please help, pyautogui always returns none for locateOnScreen. Here is my code:
import time
import sys
import pyautogui
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 1
pyautogui.size()
width, height = pyautogui.size()
y = pyautogui.locateOnScreen('LOLicon.png')
print(y)
for i in range(2):
x = pyautogui.moveTo(y)
pyautogui.click(x)
time.sleep(2)
pyautogui.doubleClick()
del x
del y
推荐答案
import time
import sys
import pyautogui
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 1
pyautogui.size()
width, height = pyautogui.size()
y = pyautogui.locateOnScreen('LOLicon.png')
print(y)
for i in range(2):
x = pyautogui.moveTo(y[0:2])
pyautogui.click(x)
time.sleep(2)
pyautogui.doubleClick()
上面的这个工作得很好.代码中唯一的变化是 x = pyautogui.moveTo(y[0:2])
.
This above works perfectly. The only change in code is x = pyautogui.moveTo(y[0:2])
.
因此请确保在您运行脚本时图像确实在屏幕上(未被代码编辑器或其他窗口隐藏)和LOLicon 的图像内容.png
实际上就是你所假设的.
So make sure that the image is actually on screen while you are running your script (not hidden by the code editor or other window) and the image content of LOLicon.png
is actually what you assume it is.
检查此代码是否也有帮助:
It could be also helpful to check out if this code:
import pyautogui
im = pyautogui.screenshot(region=(20, 20, 50, 50))
im.save("myScreenshot.png")
y = pyautogui.locateOnScreen("myScreenshot.png")
print(y)
x = pyautogui.moveTo(y[0:2])
运行良好,没有错误.如果是,您可以将 myScreenshot.png
重命名为 LOLicon.png
并调整 region=(20, 20, 50, 50)
以便,它捕获屏幕上的 LOLicon.
runs fine without an error. If it does, you can rename myScreenshot.png
to LOLicon.png
and adjust region=(20, 20, 50, 50)
so, that it captures the LOLicon on the screen.
查看https://pyautogui.readthedocs.io/en/latest/screenshot.html?highlight=save%20image 了解 pyautogui 中截图功能的详细信息,并确保安装了所需的模块( Pillow 和最终 scrot 如果你在 Linux 上).
Check out https://pyautogui.readthedocs.io/en/latest/screenshot.html?highlight=save%20image for details of the screenshot functions in pyautogui and make sure the required modules are installed ( Pillow and eventually scrot if you are on Linux ).
这篇关于PyAutoGui 始终为 locateOnScreen 返回 none的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!