how does observeEvent work exactly ?

Asked by ONG YI CHONG

So ive been reading the sikuliX api on observing events and Im not able to understand certain events that happen when i try coding it out myself.

I wrote this function that clicks a region, and after the click will observe that region for any change in pixels.

def observe_and_click(phone_region, pixel_region, handler, observe_time):
    pixel_region.click() # make pixel click on region
    phone_region.onChange(50,handler) # observe for changes in region
    phone_region.observe(observe_time) # how long to observe for

this is the handler function for the above.

def handler(event):
    global i
    r = event.getRegion() # get the observing region for this event
    path = capture(r,"/Users/ongyichong/SikuliX/Scripts",str(i))
    i += 1
    print(path)
    r.stopObserver()

So every click of an event would trigger the handler function once since at the end of the handler function i make the observation stop. However my message log prints something like this:
[log] CLICK on L(55,109)@S(0)[0,0 1440x900] (109 msec)
/Users/ongyichong/SikuliX/Scripts/0.png
/Users/ongyichong/SikuliX/Scripts/1.png

/Users/ongyichong/SikuliX/Scripts/2.png

[log] CLICK on L(75,109)@S(0)[0,0 1440x900] (101 msec)
/Users/ongyichong/SikuliX/Scripts/3.png
/Users/ongyichong/SikuliX/Scripts/4.png
/Users/ongyichong/SikuliX/Scripts/5.png

/Users/ongyichong/SikuliX/Scripts/6.png

where i get like 3 - 4 print statements for every click i make ? shouldnt Stopobserver() cause the handler to be called once per observe_and_click function call ? I have already tried reading the documentation but i cant seem to find any information that could explain this phenomenon.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
ONG YI CHONG
Solved:
Last query:
Last reply:
Revision history for this message
ONG YI CHONG (calveeen) said :
#1

Btw i am running the function Observe_and_click inside a loop and there are certain transition effects when the onChange() is called, for example a menu bar popping up on the phone when it is clicked. Could that explain the multiple handler function being called ?

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

I have to check.

In fact it should work like you thought it should work ;-)

Is the phone_region always the same Region OBJECT?

try to define the phone_region once outside the loop together with the
phone_region.onChange(50,handler) # observe for changes in region

so your function should only be:
def observe_and_click(phone_region, pixel_region, handler, observe_time):
    pixel_region.click() # make pixel click on region
    phone_region.observe(observe_time) # how long to observe for

Revision history for this message
ONG YI CHONG (calveeen) said :
#3

Yes, phone region is always the same region object. Hey your suggestion worked and it solved my problem ! Thank you so much !!!!

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

Thanks for feedback.

Daily business ;-)