Python Forum

Full Version: problem with Pyautogui
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
first of all i'm a bigginner and i have a problem with Pyautogui .

i create a simple code to find the software logo on desktop then type 'I found it ' ,and if i hide the logo the cod type 'I am unable to found it,the code works good.
but when i add another action ,which is i want the mouse move to the position of the logo . its works good ,but when i hide the logo the mouse still move to the postion ,and still type i found it , but normally the code should show me I am unable to found it.
please heelp me with an correct cod.


my code is:
import pyautogui
import time

location = pyautogui.locateOnScreen('image.png', confidence = 0.6)

while 1:

    if location:

        print("I found it ")
        time.sleep(2)
        print(pyautogui.moveTo(location))

    else:

        print("I am unable to found it")
(Jan-27-2021, 04:25 PM)rachidel07 Wrote: [ -> ]
location = pyautogui.locateOnScreen('image.png', confidence = 0.6)
while 1:
    if location:

You check for the file's location one time, when the script starts. If you hide it after starting the script, it won't "forget" where it is, because you never check for it again. If you want to continuously watch for the object, you need to continuously look for the object. Like this:
while True:
    location = pyautogui.locateOnScreen('image.png', confidence = 0.6)
    if location: