简体   繁体   English

Pyautogui找到图片后一直点击鼠标位置

[英]Pyautogui keeps clicking at the mouse location after finding the image

The code below keeps clicking on its own after finding the set image and clicking on it but after that it keeps clicking by itself on the current mouse x,y.下面的代码在找到设置的图像并单击它后会不断单击它,但之后它会在当前鼠标 x,y 上不断单击它。

How can i make it so the click will only happen 1x after it finds image.我怎样才能做到这一点,所以点击只会在找到图像后发生 1 次。

import queue
from pyautogui import *
import pyautogui
import time
import keyboard
import numpy as np
import random
import win32api, win32con

time.sleep(2)


while keyboard.is_pressed('q') == False:
    
    eventicon = pyautogui.locateOnScreen("SummerEventBot\eventicon.png")
    pyautogui.click(eventicon)

When pyautogui doesn't find the desired image, it returns None instead of coordinates.当 pyautogui 没有找到所需的图像时,它返回None而不是坐标。 When you pass None to click() , it just clicks on the current position.当您将None传递给click()时,它只会单击当前的 position。 So you need to check if you actually found the image.所以你需要检查你是否真的找到了图像。 You can do that by checking if the locate function didn't return None .您可以通过检查 locate function 是否没有返回None来做到这一点。

while keyboard.is_pressed('q') == False:
    
    eventicon = pyautogui.locateOnScreen("SummerEventBot\eventicon.png")
    if eventicon is not None:
        pyautogui.click(eventicon)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM