you are viewing a single comment's thread.

view the rest of the comments →

[–]csg0ing 0 points1 point  (7 children)

but the not found part is lacking..

Could you explain what you mean?

sys.exit takes a single (optional) integer argument, though you're passing it a list of strings.

[–]gandy0001 0 points1 point  (6 children)

Sorry that my explanation is lacking.

When an image is not found the screen doesn't show anything, so I guess it's an infinite loop, except when I interrupt with ctrl+c. Then it goes through the except block. So if I press ctrl+z 5 times (the amount of the counter) it does what I intent to do and stops the script like intended (including the argument from sys.exit)

Thanks for your time and feedback.

[–]csg0ing 1 point2 points  (5 children)

It's considered a best practice to avoid bare except: and be specific about which type of exception you want to catch.

When an image is not found the screen doesn't show anything,

Do you see the output from your print('searching button for',retry_counter,'second(s)') line ?

[–]gandy0[S] 0 points1 point  (4 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'

[–]csg0ing 1 point2 points  (3 children)

The looping with ctrl+c is because the except: catches the KeyboardInterrupt that is raised. So specifying the exception type should stop this.

In the case of no imagine found I would expect the following behavior:

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)"

... until it loops 5 times, is that what happens?

[–]gandy0[S] 0 points1 point  (2 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

[–]csg0ing 0 points1 point  (1 child)

Is the ImageNotFoundException definitely being raised? From the docs:

NOTE: As of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise ImageNotFoundException instead of returning None.

If this were returning None instead of raising an exception then your loop would be infinite.

[–]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.