简体   繁体   中英

In Python how would one wait a specific time while waiting for an image to possibly occur?

More specifically, I have a Sikuli code that works fine for the most part. However, during my 5400 second wait sometimes I have a network connection error that pops a button on the screen I need to click that occurs at no specific time during those 5400 seconds. Also, if that network button occurs and I click it I need to start the code over from the beginning.

I assume I can do a nested While loop waiting for that network connection button to pop up and if it doesn't occur continue with the code but how would I write that? Here's the code currently:

while True:
    
    exists("start.png", 10*60)
    click(Pattern("start.png").targetOffset(-1,0))
    wait("enter.png", 5)
    
    click(Pattern("enter.png").targetOffset(2,30), 5)
    wait(5400)
    type(Key.ESC)
    exists("leave.png", 5)   
    click(Pattern("leave.png").targetOffset(-11,0))
   #the following if statements could or could not occur and could come in no specific order. This part of the code could be incorrect
    if exists("close1.png", 5):
       click(Pattern("close1.png").targetOffset(0,1))
    elif exists("close2.png", 20):
        click("close2.png")
    elif exists("close3.png", 20):
        click("close3.png")
    wait(5)

A suggestion from RaiMan from SikuliX (not tested;-)

""" 
#the following if statements could or could not occur and could come in no specific order. 
#This part of the code could be incorrect
if exists("close1.png", 5):
   click(Pattern("close1.png").targetOffset(0,1))
elif exists("close2.png", 20):
    click("close2.png")
elif exists("close3.png", 20):
    click("close3.png")
"""
netError = False

def handler(event):
    global netError
    netError = True
    click(event.getMatch())

onAppear("close1.png", handler)
onAppear("close2.png", handler)
onAppear("close3.png", handler)

while True:

    netError = False
    observeInBackground(FOREVER)

    #exists("start.png", 10*60)
    #click(Pattern("start.png").targetOffset(-1,0))
    click(wait("start.png", 1))
        
    #wait("enter.png", 5)   
    #click(Pattern("enter.png").targetOffset(2,30), 5)
    click(wait(Pattern("enter.png").targetOffset(2,30), 5)

    towait = 5400
    while not netError and toWait > 0:
        wait(1)
        towait -= 1
        
    if netError:
        stopObserver()
        continue
        
    type(Key.ESC)

    #exists("leave.png", 5)   
    #click(Pattern("leave.png").targetOffset(-11,0))
    click(wait(Pattern("leave.png").targetOffset(-11,0), 5)

    stopObserver()
    wait(5)
        

Here is some alternative without observer events. I personally find it simpler to work in this way:-). I used your original code and just replaced wait(5400) with a time based loop that checks for images that might occur.

import time

while True:
    
    exists("start.png", 10*60)
    click(Pattern("start.png").targetOffset(-1,0))
    wait("enter.png", 5)
    
    click(Pattern("enter.png").targetOffset(2,30), 5)
    
    t0 = time.time()
    while (time.time()-t0 < 5400):
   
        #the following if statements could or could not occur and could come in no specific order. This part of the code could be incorrect
        if exists("close1.png", 5):
            click(Pattern("close1.png").targetOffset(0,1))
        elif exists("close2.png", 20):
            click("close2.png")
        elif exists("close3.png", 20):
            click("close3.png")

    type(Key.ESC)
    exists("leave.png", 5)   
    click(Pattern("leave.png").targetOffset(-11,0))
    wait(5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM