Observing Multiple Regions Simultaneously

Asked by Lebron Jones

Hello,

I'm trying to watch for one of any number of windows to appear. Previously, I simply observed for all of them on the entire screen:

imgOne = "imgOne.png"
imgTwo = "imgTwo.png"
imgThree = "imgThree.png"

onAppear(imgOne, handlerOne)
onAppear(imgTwo, handlerTwo)
onAppear(imgThree, handlerThree)

while (True):
    Observe(Forever).

But, now I want the script to run faster, so I've cut down the regions that each window could appear in:

"""
imgOne = "img1.png"
imgTwo = "img2.png"
imgThree = "img3.png"
rOne = Region(0,0,200,200)
rTwo = Region(200,0,200,200)
rThree = Region(400,0,200,200)

rOne.onAppear(imgOne, handlerOne)
rTwo.onAppear(imgTwo, handlerTwo)
rThree.onAppear(imgThree, handlerThree)
"""

xloc = 0
for x in ["One", "Two", "Three"]:
    img = "img" + x + ".png"
    reg = Region(200*xloc, 0, 200, 200)
    reg.onAppear(img, handler)
    xloc++

While(True):
    observe(Forever)

But, it seems like the call to Observe isn't working (for either of the methods above, the old way of hand coding each region, or the new dynamic one). I believe its because Observe only refers to events tied to the entire screen, and not to any specific region that has been created. But, the documentation isn't very deep on the subject. Ideally, I would like to be able to keep everything dynamic in the for loop, because I may want to add images later and it just makes for convenient code.

I tried calling observe on each region individually:

for reg in regs:
    reg.observe(Forever)

But its a blocking call, so only one image would be looked for at a time. Is there a way to observe multiple regions at one time, or do I need to switch the backbone to a loop of

while (True):
    for img in imgs:
         if (reg.exists(img)): handle(img)

? I'd rather not, Observe seems more elegant.

Question information

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

everything you did is the way, how to gain performance with Sikuli.

But the plain observe(FOREVER) is indeed blocking: it waits until a stopObserver() is called in the handler. So the script itself meanwhile hangs.

So if you want to observe different regions in parallel, all observe calls except the last one (if needed) have to use the background feature:
region.observe(FOREVER, True)
or
region.observe(FOREVER, background=True)

Can you help with this problem?

Provide an answer of your own, or ask Lebron Jones for more information if necessary.

To post a message you must log in.