← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #149688]: Multiple Observers

 

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

    Status: Open => Answered

RaiMan proposed the following answer:
how to repeat something: faq 1437

about observe you should first look at:
--1. http://sikuli.org/docx/region.html
--2. http://sikuli.org/docx/region.html#finding-inside-a-region-and-waiting-for-a-visual-event
--3. http://sikuli.org/docx/region.html#observing-visual-events-in-a-region

general comment:
the observer is convenient, if you want to look for visual objects in parallel and if reaction timing is the focus. But it is relatively complex to implement,because of the communication between your script and the event handlers.

To get a start it is mostly sufficient to start with a while loop, that
uses exists().

example:

img1 = "some_image1.png"
img2 = "some_image2.png"
img3 = "some_image3.png"

imgstop = "some_other_image.png"
# to stop the loop, make imgstop invisible

isImg1 = False
isImg2 = False
isImg3 = False

while exists(imgstop, 0): # loop as long imgstop is visible
   if exists(img1, 0):
      isImg1 = True
   if exists(img2, 0):
      isImg2 = True
   if exists(img3, 0):
      isImg3 = True
   if not (isImg1 or isImg2 or isImg3): # no img visible?
      wait(1) # time to be adjusted
      continue # we look again
   # at this point we know which images are present
   # some code to do something depending on
   # which images exist e.g.
   if isImg1 and isImg2:
      # do something
   #prepare to loop again
   isImg1 = False
   isImg2 = False
   isImg3 = False
   wait(1) # time to be adjusted
   # we go back to loop again
# first statement after the loop

I think for up to 5 visual objects this could work rather good,
especially when the search is restricted to a smaller region (e.g. app
window). The second parameter 0 in exists() makes the search to come
back after the first try (0.3 seconds in average) and not wait the
standard 3 seconds.

You may try to convert the "if exists()" to onAppear(img, handler). The
challenge would be to know inside a handler, wether one of the other
events has happened meanwhile (global variables or added region
attributes).

Finally: yes, the complexity of your goal needs some diving into the
theoretics of Python scripting and the Sikuli API.

Have fun though ;-)

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