Wait for event in desktop surveillance for specified time and do something if timeout

Asked by Casper Pold

I'm trying to make a desktop surveillance that clicks a bottom if the expected pattern does not show up within a specified time. The desktop surveillance should then continue looking for the expected pattern, to see if the action caused the expected pattern to show up within the next time interval.
I would expect the following to loop until X shows up, and then click X. If X does not show up within 10 sec, Y should be clicked and the loop should restart. Note: If X appears, also Y disappears.

The following code clicks X and stops as it should if X is found, but it does not click Y if X is not found within 10 sec, but keeps waiting doing nothing. I'm not sure what I'm missing here. Please help.

found_X = False
times_not_found_X = 0
while found_X == False:
    if exists("X.png"):
        click("X.png")
        found_X = True
    else:
        if times_not_found_X<= 10:
            wait(1)
            times_not_found_X += 1
        else:
            click("Y.png")
            times_not_found_X = 0

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

while True:
    if exists("X.png", 10):
        click("X.png")
        break # the loop is terminated here
    else:
        click("Y.png")

Revision history for this message
Casper Pold (casper-5) said :
#2

Thanks RaiMan, that solved my question.