Home > Mobile >  Pyautogui keeps clicking at the mouse location after finding the image
Pyautogui keeps clicking at the mouse location after finding the image

Time:08-12

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.

How can i make it so the click will only happen 1x after it finds image.

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)

CodePudding user response:

When pyautogui doesn't find the desired image, it returns None instead of coordinates. When you pass None to click(), it just clicks on the current 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.

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