Trouble figuring out onAppear

Asked by Sao

I have read a lot of threads about how to use onEvents but I still can't get my head around it.

The part of the code that should contain this looks a bit like this

while tot < x:
    do stuff
    x += 1

I want it to keep doing stuff unless tot reaches the value of x OR if an image appears (pop up). From what I've read you can let sikuli observe a region in the background for that pop up so the "do stuff" still continues. Why I don't use if not exists() for example is purely a matter of speed.

Some help figuring this out would be greatly appreciated

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
masuo (masuo-ohara) said :
#1

I think that there is not any codes to break from while loop.
For example, change global variable in the handler of onAppear and use the variable to the branch condition.

By the way, I do not think that onAppear contributes to the speed than exists.
Why do you think that onAppear contributes to the speed than exists?

Revision history for this message
Sao (e-bm) said :
#2

My thought was that since onAppear can work in the background it doesn't stop the script like a exists would.
If I do it like this it stops to check if png.1 is there after each "do stuff" round. I want that process to be done quicker.

while tot < x and not exists(png.1):
    do stuff
    x += 1

Revision history for this message
masuo (masuo-ohara) said :
#3

If you do not wait 3 seconds by using exists() when png.1 is not exist, you may use exists("png.1",0)
http://sikulix-2014.readthedocs.io/en/latest/region.html?highlight=exists#Region.exists

Revision history for this message
Karl (k-d) said :
#4

Python doesn't use use the += syntax. What language are you using?

onAppear uses a handler with an associated event that observe() throws to start that handler.

def myHandler(event):
    print "myImage just appeared"
onAppear("myimage.png", myHandler) #Note: this isn't a function call is where the function is
observe(60, background=True) #Deprecated background, use observeInBackground

http://sikulix-2014.readthedocs.io/en/latest/region.html?highlight=observe#Region.observeInBackground

Revision history for this message
Karl (k-d) said :
#5

breakLoop = False

def myHandler(event):
    global breakLoop
    breakLoop = True
    print "myImage just appeared"

onAppear("myImage.png", myHandler)
observe(60, background=True)

x = 0
while x < 10000 and not breakLoop:
    print x
    x = x+1

Can you help with this problem?

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

To post a message you must log in.