← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #169767]: IF image appears FIRST in 1 of the 2 regions

 

Question #169767 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/169767

    Status: Open => Answered

RaiMan proposed the following answer:
Excuse me, if I am wrong, but it seems, that you do not really understand, what is happening here.
... and you do not really use my suggestions :-(

You need some understanding of scripting generally and Python basics.

I explain your snippet, hoping you understand that it is not really
doing something of value and hoping you try to understand, what my
suggested script from comment #5.

- a line beginning with a # is a comment in Python
- on a line containing a statement, text after # is comment
- I put comments in your code and refer to them in the following comments

The most basic thing in Python is to understand indenting, which is
Pythons feature to form a block of statements: all consecutive
statements with the exactly the same indentation form a block. blocks
can be nested.

while True: # loop
   # loop block start
   # if
   if regA.exists(imgA): # exists 1
      # if block start
      print "script for regA"
      # if block end
   if not regA.exists(imgA): #exists 2
      # if block start
      print "script for regB"
      wait(5) # wait
      # if block end
   # loop block end

the comments:

-- # loop
while True: is the simplest construct, to say, that the following block (the statements between # loop block start and # loop block start) should be repeated forever.
You can replace True by any expression that evaluates to False or True. In this case  the loop will run as long the condition evaluates to True.

-- # if
generally

if condition:
    pass # processed if condition evaluates to True
else:
    pass # processed if condition evaluates to False

pass is Python and means "do nothing", but it assures indentation for
correct syntax.

since Python does not have a case statement, you can use intermediate
elif's:

if condition:
    pass # processed if condition evaluates to True
elif condition1:
    pass # processed if condition1 evaluates to True
elif condition2:
    pass # processed if condition2 evaluates to True
# more elif's possible 
else:
    pass # processed if all of the above conditions evaluate to False

-- # exists 1
regA.exists(imgA) repeatedly searches for image imgA in region regA until found or the standard waiting time of 3 seconds elapses. returns True if found, otherwise False.
That it makes sense, to use exists() in decisions like yours, it makes sense to use the option, that exists returns after the first try to find the image (usually earlier than 0.5 seconds).
So I recommend to use:
if regA.exists(imgA, 0):

-- #exists 2
here you start a subsequent search for thee same thing as before with # exists 1. But now you state, that if the imgA is not found within 3 seconds, then we have a situation "script for regB". I doubt that this makes any sense, since you do not know here the situation in regB.

If it would make sense, it should read:

  if regA.exists(imgA): # exists 1
      # if block start
      print "script for regA"
      # if block end
  else:
      # else block start
      print "script for regB"
      wait(5) # wait
      # else block end

# wait
the position of the wait(5) leads to that it is only processed, when imgA is not found. If imgA is found, the loop instantly starts all over again.

--- So I hope with this you might have a chance to understand my
suggestion

regA = Region(x,y,w,h) # to be specified or evaluated somehow
regB = Region(x,y,w,h) # to be specified or evaluated somehow
imgA = "the captured image A you are waiting for.png" # here you should see the thumbnail!
imgB = "the captured image B you are waiting for.png"

while True: # this loops for ever
    # loop 1
    isRegA = False
    isRegB = False
    while True:
        # loop 1
        # look for imgA, set isRegA to True if found
        if regA.exists(imgA, 0): isRegA = True
        # look for imgB, set isRegB to True if found
        if regB.exists(imgB, 0): isRegB = True
        # check isRegA and isRegB
        # if one of it is True, leave the loop
        if isRegA or isRegB: break # one or both images appeared
        wait(5) # wait 5 seconds and start over again at # loop 2

    # loop 1 has ended, we check the result
    if isRegA: # imgA was found if isRegA is True
        print "running script for A"
    elif isRegB: # imgB was found if isRegB is True
        print "running script for B"
    else:
        # this should never be printed, since loop 2 is only left,
        # if either imgA or imgB is found
        print "should never happen"
    # we start over at # loop 1

# this is the end of the main loop (# loop 1)
print "this will not be printed, since we never come here - loop 1 is forever"

You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.