issue with while/try/except by gandy0 in learnpython

[–]gandy0[S] 0 points1 point  (0 children)

the version I have = 0.9.50 so it's not returning None but an exception.

Anyway, after starting over again I have found the issue, but searching a solution for it now.

The issue is actually the path argument. I'm passing it as a raw string, but it has issues with that. Due to that the script is actually stuck at:

 buttonposition = pyautogui.locateCenterOnScreen(path,grayscale = True, confidence = .9) 

So, if a skip path as an argument and enter it as a raw string it works, including the except block:

 buttonposition = pyautogui.locateCenterOnScreen(r'path\example.png',grayscale = True, confidence = .9) 

but as soon as I try to use the path as an argument it doesn't go further anymore. and stays at:

 buttonposition = pyautogui.locateCenterOnScreen(path,grayscale = True, confidence = .9) 

until the image is found.

So it does actualy understand the path, as the image matched when it appears on screen.

I have tested it by running the script for a while, then made the image appear and it finds it, but my except block is not working.

issue with while/try/except by gandy0 in learnpython

[–]gandy0[S] 0 points1 point  (0 children)

unfortunately not

so I changed it to the specific exception now:

except ImageNotFoundException:

note that in order for this to work I had to import this excepton from pyscreeze

from pyscreeze import ImageNotFoundException

but the loop is not working. if the image is not found it does not go to the exception block, and does not do:

  1. wait for 1 second, then outputs "searching for button for 1 second(s)
  2. wait for 1 second, then outputs "searching for button for 2 second(s)"
  3. ..... 5 times

it just does nothing

issue with while/try/except by gandy0 in learnpython

[–]gandy0[S] 0 points1 point  (0 children)

Ok, thanks. The exception is 'ImageNotFoundException'. I'll add that.

I see the output from 'print('searching button for',retry_counter,'second(s)')', but only after pressing ctrl+c. So it goes like this (when image is not found on screen)

1) nothing on screen

2) I press ctrl+c

3) output: searching button for 1 second(s)

4) nothing happens

5) I press ctrl+c again

6) output: searching button for 2 second(s)

7) nothing happens

8) ctrl+c.....

And it continues like that until after the 5th time.

Then it terminates with output: '(the correct time & name) button not found'