How to use functions with a default region

Asked by Michiel Vanthuyne

Hi,

I'm trying to create a function where the caller can optionally pass a region, if no region is passed the Screen region should be used as default, but this is not working. What am I doing wrong?

This is my function:

def tryClick(img,reg=Screen, delay = 5):
    if reg.exists(img,delay):
        reg.click(reg.getLastMatch())
        return True
    else:
        print("tryClick failed for pattern" +str(img))
        return False

calling lines might be something like:

tryClick("img.png") #use Screen as region
tryClick("img2.png", myRegion) #use myRegion as region
tryClick("img3.png",myRegion2,60) #use myRegion2 and wait for one minute

I get an error like below when calling the function using the first line:
 File "C:\Users\ADMINI~1\AppData\Local\Temp\sikuli-tmp4130329311333098074.py", line 68, in tryClick
 if reg.exists(img,delay):
TypeError: exists(): self arg can't be coerced to org.sikuli.script.Region

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
Eugene S (shragovich) said :
#1

Hi,

To create a new Screen object you must pass a screen id as a parameter:

Screen([id])

You can read that in the documentation:
http://doc.sikuli.org/screen.html#Screen.Screen

If you have a single screen setup, that will be 0, so your function will become:

def tryClick(img,reg=Screen(0), delay = 5)

Eugene

Can you help with this problem?

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

To post a message you must log in.