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
#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


Messages In This Thread
RE: PyautoGUI- How to use If - Else with pyautogui.locateCenterOnScreen - by DeaD_EyE - Oct-05-2021, 01:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyautogui, İmagesearch, Moving target Beyazx 0 689 Jun-27-2023, 08:47 PM
Last Post: Beyazx
  PyAutogui write Dollar Sign Dutch keyboard not working alato 0 888 Nov-22-2022, 11:25 PM
Last Post: alato
  pyautogui.locateOnScreen producing error dude8074 6 4,241 Apr-17-2022, 05:05 PM
Last Post: bowlofred
  how to take a screnshot by Pyautogui automatically and randomly rachidel07 0 3,708 Feb-03-2021, 01:16 PM
Last Post: rachidel07
Sad problem with Pyautogui rachidel07 1 2,583 Jan-27-2021, 05:43 PM
Last Post: nilamo
  pyautogui with a display emulator? gumby4231 0 2,732 Jul-30-2020, 02:46 PM
Last Post: gumby4231
  pyautogui.screenshot region is not working alexlu 6 6,809 Jun-04-2020, 10:46 AM
Last Post: alexlu
  loop in pyautogui (python automation GUI application) pyprogrammer 0 4,956 Feb-12-2020, 02:52 PM
Last Post: pyprogrammer
  Doesn't work function pyautogui.typewrite() aliyevmiras 1 4,989 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,633 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