Pyautogui keeps clicking at the mouse location after finding the image

Question:

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("SummerEventBoteventicon.png")
    pyautogui.click(eventicon)

Asked By: Pepga77x

||

Answers:

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("SummerEventBoteventicon.png")
    if eventicon is not None:
        pyautogui.click(eventicon)
Answered By: Filip Müller
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.