Sikuli Doc - Example code returns error

Asked by xyz_User on 2019-10-20

Was just reading this:
http://doc.sikuli.org/region.html#observing-visual-events-in-a-region

copy pasted the code in sikuli:

def changed(event):
        print "something changed in ", event.region
        for ch in event.changes:
                ch.highlight() # highlight all changes
        sleep(1)
        for ch in event.changes:
                ch.highlight() # turn off the highlights
with selectRegion("select a region to observe") as r:
    # any change in r larger than 50 pixels would trigger the changed function
    onChange(50, changed)
    observe(background=True)

wait(30) # another way to observe for 30 seconds
r.stopObserver()

and I'm getting the following error:

[error] script [ Untitled ] stopped with error at line --unknown--
[error] Error caused by: Traceback (most recent call last): File "C:\Users\testUser\AppData\Local\Temp\Sikulix_367046279\sikuli-2894467953142955615.py", line 1, in <module> def changed(event): AttributeError: 'org.sikuli.script.Region' object has no attribute '__exit__'

Question information

Language:
English Edit question
Status:
Expired
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2019-10-21
Last reply:
2019-11-06

This question was reopened

xyz_User (userseven) said : #1

Found anther topic and with a bit of modification this seems to work:

region = Region(12,152,1014,477)
print "scanning specified region"
r = region
r.onChange(1, changed)
r.observe(background=True)
Settings.isChanged = False
r.observe(3) #observes for 5 seconds
if Settings.isChanged:
    print "there were changes detected"
else:
    print "No changes detected"
def changed(event):
    for ch in event.changes:
        ch.highlight() # highlight all changes

---------------------------------------------

So in 3 seconds, it finds and highlights the changes, the problem is I would like to simply click on the location of ONE of the random changes

If i set it to:

def changed(event):
    for ch in event.changes:
        ch.click() #Click on the changes

it'll click on every single area that changed, I would like sikuli to only click the first change that it saw or just one of the random change.

I hope that makes sense
Thanks

RaiMan (raimund-hocke) said : #2

def changed(event):
    for ch in event.changes:
        ch.click() #Click on the changes
        break

this will end the loop after the first click

... or you might try:
click(event.changes[0])

xyz_User (userseven) said : #3

I've tried both. Still seems to click all of the changed locations:

here's the log for both suggestion above:

scanning specified region
[log] CLICK on L(648,365)@S(0)[0,0 1366x768] (25 msec)
[log] CLICK on L(628,479)@S(0)[0,0 1366x768] (46 msec)
[log] CLICK on L(628,479)@S(0)[0,0 1366x768] (51 msec)
[log] CLICK on L(628,480)@S(0)[0,0 1366x768] (45 msec)

RaiMan (raimund-hocke) said : #4

not sure, what happened on your side.

this works as expected:

def changed(event):
  print "something changed ", len(event.changes)
  event.stopObserver()
  for chg in event.changes:
    chg.highlight()
  wait(2)
  for chg in event.changes:
    chg.highlight()
  print "click first"
  for chg in event.changes:
    chg.click()
    break
  print "click last"
  event.changes[-1].click()

img = "img.png"
m = find(img).grow(0, 150, 0, 0).below(200)
m.highlight(1)
m.onChange(10, changed)
m.observe(10)

I am taking care that 2 changes are detected - the print

[debug] Observer: changes: 2 in: R[46,121 258x200]@S(0)
[debug] Observer: running call back
something changed 2
[debug] Region: observe: request to stop observer for R[46,121 258x200]@S(0)
[debug] highlight M[102,153 14x8]On(0) S 10000 for 0.0 secs
[debug] highlight M[46,121 257x199]On(0) S 10000 for 0.0 secs
click first
[debug] CLICK on L[109,157]@S(0) (529 msec)
click last
[debug] CLICK on L[174,220]@S(0) (531 msec)
[debug] Observer: update result: Changes: true
[debug] Region: observe: ended successfully: R[46,121 258x200]@S(0)

xyz_User (userseven) said : #8

Thanks RaiMan, that solved my question.

Launchpad Janitor (janitor) said : #10

This question was expired because it remained in the 'Open' state without activity for the last 15 days.