[Python] Is there away to add an exception for ImageNotFoundException in a While true loop?

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    is there away to fix this exception error I'm getting? Its likely something to do with the "While" loop but I have no idea how to fix this.


    raise ImageNotFoundException # Raise PyAutoGUI's ImageNotFoundException. pyautogui.ImageNotFoundException
    ​

    Code to focus on:

    call_count = 0
    if pyautogui.locateOnScreen('1.png', confidence=0.9):
    while call_count <= 7:
    logging.info(call_count)
    call_count += 1
    if has_restarted:
    return
    restarting_function(True)
    restarting_function()
    if pyautogui.ImageNotFoundException:
    pass


    Full Code

    import pyautogui
    import schedule
    import time
    import logging

    def restarting_function(has_restarted=False):
    logging.basicConfig(level=logging.INFO, filename="anomaly.log", filemode="w", format="%(asctime)s - %(levelname)s - %(message)s")
    logger = logging.getLogger(__name__)
    logger.propagate = False

    handler = logging.FileHandler('anomaly.log')
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    handler.setFormatter(formatter)

    logger.addHandler(handler)
    call_count = 0
    if pyautogui.locateOnScreen('1.png', confidence=0.9):
    while call_count <= 7:
    logging.info(call_count)
    call_count += 1
    if has_restarted:
    return
    restarting_function(True)
    restarting_function()
    except pyautogui.ImageNotFoundException:
    pass

    # schedule for main working code
    schedule.every(1).seconds.do(restarting_function)

    while True:
    schedule.run_pending()
    time.sleep(1)

    Continue reading...

Compartilhe esta Página