how to use onAppear in a region

Asked by Jonathan

Hi,

I am currently running Sikuli version X-1.0rc2 on windows and am trying to make a popup window occur when an event is seen on the screen.

My code is as follows:

### Start of code ###

def myHandler(event):
        textbox=popup(" it")
        pass

Region(226,90,350,105).onAppear(image1 , myHandler)

observe(5)
### End of code ###

So whenever image1 is seen on the screen in the Region(226,90,350,105) I am expecting a popup to be displayed. However this is not happening.

Is there something I am doing incorrectly?

Any help is appreciated

Thanks

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

you got it --- nearly ;-)

the onAppear is bound to a Region, but the observe() is bound to SCREEN (the default region).

optimized and corrected:

def myHandler(event):
        popup(" it") # popup() does not return anything / pass only necessary if otherwise not any code in the def()

reg = Region(226,90,350,105)
reg.onAppear(image1 , myHandler)
reg.observe(5)

when the script reaches observe(5) it stops running for 5 seconds. during this time the onAppear() event is watched out in the Region reg. if it happens, during this 5 seconds, the popup should come up.

you might use with:

reg = Region(226,90,350,105)
with reg:
    onAppear(image1 , myHandler)
    observe(5)

Revision history for this message
RaiMan (raimund-hocke) said :
#2

I hate these line breaks in this board :-<>

def myHandler(event):
        popup(" it") # popup() does not return anything
        # pass only necessary if otherwise not any code in the def()

Revision history for this message
Jonathan (jonathanc-stewart) said :
#3

Hi RaiMan

Thanks for the solution, a further question how do you stop the observer after it has found an occurrence of what you are looking for?

Is it .....
reg.observer(5)
reg.stopObserver()

Thanks

Revision history for this message
RaiMan (raimund-hocke) said :
#4

Since there was added class SikuliEvent in RC2, you should read: http://sikuli.org/docx/region.html#observing-visual-events-in-a-region

with this construct

reg.observer(5)
reg.stopObserver()

your script will pause for 5 seconds processing the observe(), so the stopObserver() will do nothing, when executed, since the observer is already stopped. You may stop the observer in the handler in this case, to continue your script in the moment of the observed event happening:

def myHandler(event):
        popup(" it")
        event.region.stopObserver()

as already mentioned, class SikuliEvent gives more options to handle events in the case more than one is observed simultaneously.

For only one event, this would be equivalent:

for i in range(5):
    if reg.exists(<some-image>, 0)
         break
    wait(0.7)

comments:
-- exists(<some-image>, 0)
looks only once and comes back
-- wait(0.7)
each find costs in average about 0.3 seconds, so each loop step would last about 1 second.

using observe() makes only sense if you have more than one event you are waiting for at the same moment even in different regions (each region can have it's own observer) or if you want to go on with your script in parallel, while the observer is running (observe(FOREVER, background=True))

Revision history for this message
Shivakumar (shivaselenium) said :
#5

can i know for java, how i need to write the lines of code "with reg:
    onAppear(image1 , myHandler)
    observe(5)"

Revision history for this message
RaiMan (raimund-hocke) said :
#6

@ shivaselenium

start reading here:
https://answers.launchpad.net/sikuli/+question/191821

Can you help with this problem?

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

To post a message you must log in.