Python Forum
PyautoGUI- How to use If - Else with pyautogui.locateCenterOnScreen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyautoGUI- How to use If - Else with pyautogui.locateCenterOnScreen
#1
Essentially I want my Python script to perform this...

     x = True
     if x  == True:
         print('Congratulations, you won!')
     else:
         print('I'm sorry but you lost.')
with this...

     a, b = pyautogui.locateCenterOnScreen('~/home/file_01.png', region=(1065,250,385,700), confidence=0.9)

In other words, how can I have my script check to see if PyautoGUI has successfully located the center of ~/home/file_01.png?

See, about 95% of the time the following works correctly....

     a, b = pyautogui.locateCenterOnScreen('~/home/file_01.png', region=(1065,250,385,700), confidence=0.9)
But about 5% of the time ~/home/file_01.png does not exist. In those cases my script stops and throws off the following error message...

     TypeError: cannot unpack non-iterable NoneType object
Instead of having my script stops in cases when ~/home/file_01.png does not exist I would prefer that my script skip that instance. In other words, instead of stopping I want my script to ignore cases when ~/home/file_01.png does not exist.
Reply
#2
The cause is that pyautogui.locateCenterOnScreen return a Point object or None if the image was not found on screen.

You already got the right Exception, which you can catch.
import pyautogui


try:
    x, y = pyautogui.locateCenterOnScreen("your_image.png")
except TypeError:
    print("Could not locate the image")
    # this is only executed, if a TypeError was caught
    # for example you can exit here with sys.exit(1)
    # or with raise SystemExit("Sorry, image not found")
else:
    # this block is executed, if no exception happened
    print(f"All fine, got the coordinates: {x} | {y}")
Tiel likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thank you very much!

Your solution worked perfectly on my first two tries.

First I ran your solution without the image visible and second, of course, I ran it with the image visible.
Reply
#4
Here is an additional naive solution without using Exceptions:
import pyautogui
 
coords = pyautogui.locateCenterOnScreen("your_image.png")

# coords is None -> no result
if coords is None:
    print("Could not locate the image")
else:
    x, y = coords  # tuple unpacking
    print(f"All fine, got the coordinates: {x} | {y}")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyautogui, İmagesearch, Moving target Beyazx 0 594 Jun-27-2023, 08:47 PM
Last Post: Beyazx
  PyAutogui write Dollar Sign Dutch keyboard not working alato 0 805 Nov-22-2022, 11:25 PM
Last Post: alato
  pyautogui.locateOnScreen producing error dude8074 6 3,865 Apr-17-2022, 05:05 PM
Last Post: bowlofred
  how to take a screnshot by Pyautogui automatically and randomly rachidel07 0 3,563 Feb-03-2021, 01:16 PM
Last Post: rachidel07
Sad problem with Pyautogui rachidel07 1 2,478 Jan-27-2021, 05:43 PM
Last Post: nilamo
  pyautogui with a display emulator? gumby4231 0 2,612 Jul-30-2020, 02:46 PM
Last Post: gumby4231
  pyautogui.screenshot region is not working alexlu 6 6,530 Jun-04-2020, 10:46 AM
Last Post: alexlu
  loop in pyautogui (python automation GUI application) pyprogrammer 0 4,810 Feb-12-2020, 02:52 PM
Last Post: pyprogrammer
  Doesn't work function pyautogui.typewrite() aliyevmiras 1 4,820 Dec-22-2019, 11:35 AM
Last Post: aliyevmiras
  Pyautogui script runs fine if split into two parts together it does not Bmart6969 1 2,550 Oct-07-2019, 10:53 PM
Last Post: Bmart6969

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020