Observing a region does nothing

Asked by Ned

Hello,

I have been trying to get a simple Observer up and running but while it compiles okay, nothing happens when I run it.

Here is the code:

from sikuli import *
import os

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

myRegion = Region(find("winStart.png").right(1547))
myRegion.onAppear("skype.png", myHandler)
myRegion.observe(FOREVER, background = False)

What I am testing here is that when the Skype Login screen appears on the taskbar, the handler should kick in. However the program just executes and finished immediately. Any help is appreciated.

Here are some images to illustrate:
http://goo.gl/r31qPj
http://goo.gl/lN8zQn
http://goo.gl/SdtAAi

Question information

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

if you are using 1.0.1 (known bug)

do not use FOREVER, but a large number instead.

Be aware: the inline observe (background=False) just waits until one of the region's events happen.
Using a long time or even FOREVER (works in 1.1.0) might lead to a hang, if the image never appears, having the need to kill sikuli.

Revision history for this message
Ned (nedleonard) said :
#2

I changed the wait to 100, but the application still exits immediately instead of waiting to observe the region. If however I have the picture present in the region when I run the test it does report that it was found and exits. Unfortunately, that seems to be the only way to get a result from it :)

Revision history for this message
Ned (nedleonard) said :
#3

Sorry, I think I know how it works now. Thanks! Obviously it needs more code to keep the program running.

Revision history for this message
Ned (nedleonard) said :
#4

Actually, it still doesn't work but I have since found out that observe() is problematic with Windows so will scrap it for now

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

a simple replacement for the the inline observe is the following:

def myObserve(region, image, wait):
    if not region: region = SCREEN
    start = time.time()
    end = start + wait
    found = True
    while not region.exists(image, 0):
        if time.time() > end:
            found = False
            break
        wait(1)
    return found

if not myObserve(some_region, some_image, 100):
    print some_image, "did not appear"
    exit(1) # or whatever you want to do here
# do what should be done if image appeared

even the background observe can be replaced similarly using threading.

BTW: with version 1.1.0 the observe definitely works and has some more options.