How to create a Region from a Pattern?

Asked by Mark Teixeira

I'm working on porting all my sikuli scripts into PyUnit in eclipse on Win7. What was working in the Sikuli IDE, and from the command line, before,

click(Pattern(w7utils.image_path + "menu_home_button.png").similar(0.60))

I've ported to,

sikuli.Sikuli.Region.click(sikuli.Sikuli.Pattern(w7utils.image_path + "menu_home_button.png").similar(0.60))

but this gives me an error:

Traceback (most recent call last):
  File "C:\Users\Fred\TestRenameRoom.py", line 20, in testRenameRoomFF
    sikuli.Sikuli.Region.click(edu.mit.csail.uid.Pattern(w7utils.image_path
TypeError: unbound method click() must be called with Region instance as first argument (got edu.mit.csail.uid.Pattern instance instead)

It seems click() needs a Region to act on, but I can't get that from Pattern(). What can I do?

Question information

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

when working with another IDE like Eclipse or Netbeans, after having put the path to sikuli-script.jar into the relevant plugin path entries (there are other Q&A, talking about that), you should put a

from sikuli.Sikuli import *

at the beginning of a script (which in fact additionally initializes the Sikuli environment).

So you can run your scripts unmodified in these IDE's (no need to qualify).

In your case, it seems that you are calling a class method Region.click(), that needs a region as parameter (unbound means it is not an instance method). The variants of calling click() seems only to be available on the instance level.

There is a globalization mechanism implemented, that during initialization (at time of import) brings all relevant methods in a state, that unqualified usage binds them to SCREEN which is a constant instance of Screen(0). This is a convenience. So even if you say click() the method is bound to an instance of class Screen.

But still in your case, you could repair your problem
(but I recommend the above approach, yours really looks ugly, isn't it ;-):

sikuli.Sikuli.Region.click(sikuli.Sikuli.Region.find(sikuli.Sikuli.Pattern(w7utils.image_path + "menu_home_button.png").similar(0.60)))

or

scr = sikuli.Sikuli.Screen(0)
scr.click(sikuli.Sikuli.Pattern(w7utils.image_path + "menu_home_button.png").similar(0.60))

With Eclipse I did not find a way yet, to tell the Eclipse/PythonPlugin syntax checker to know the Sikuli methods. So you have to live with the "unknown method" hints in the line number column. Netbeans does not have this problem. May be eclipse is more strict with the unqualified usage of methods (which is only solved by Sikuli itself at runtime).

Revision history for this message
Mark Teixeira (m-lp-ql-m) said :
#2

Thanks, you are da (Rai)Man, RaiMan!

I agree it's ugly. I could have sworn that I had "from sikuli.Sikuli import *" and sikuli-script.jar in my P.A.T.H., and "click()" was coming up with an error. Today, though, it's working fine.

I appreciate your explanation of the globalization mechanism, though. :)

Revision history for this message
Mark Teixeira (m-lp-ql-m) said :
#3

Thanks RaiMan, that solved my question.