[HowTo] use functions with parameters

Asked by Calle Rundgren

I want my script to be as universal as possible. For example I want to find a image on screen, in order to make it easy to switch and search for another image I think it would be great to use parameters.

class findGuiElement:
    def __init__(self, image1, image2, image3):
        self.image1 = image1()
        self.image2 = image2()
        self.image3 = image3()
    def findElement(image1, image2, image3):
        if exists("a universal str for image"):
            print("image found")

When calling the function I think it would be something like this:

findGuiElement(image1)

If I for example want to find image1.

Does anyone know how to execute this properly?

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
Calle Rundgren (c-rundgren) said :
#1

This may not be the best explanation. Here is a little better one:

def findElement(self, image1, image2, image3): #different images I want to have in my function
    if exists(one of the images)
        click(getLastMatch)

To call this function I would use this code in another file to where I imported the function.

findElement(image1) #here the wanted image is declared. I want to search for image1.

Which is the right solution if i want the script to search for the image I have putted as a parameter in where i executed the script? .

This would be nice becouse that I only have to write one function and just adjust which image I want to search for.

This is just a example, I think this could be used on longer scripts aswell.

Have a nice day1 :-)

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

OK, you already reduced this to a function (no need to wrap this into a class).

--- put this function definition somewhere, so it is processed before used the first time (might be imported ;-)

def clickElement(img): #different images I want to have in my function
    if exists(img): click(getLastMatch()); return(getLastMatch())
    else: return None

--- usage simple:
clickElement("some-image.png")

--- usage complex:
img = "some-image.png"
m = clickElement(img)
if not m: print img, "not found"; exit()

--- I am having a nice day :-))

Revision history for this message
Calle Rundgren (c-rundgren) said :
#3

Thanks RaiMan, that solved my question.

Revision history for this message
Calle Rundgren (c-rundgren) said :
#4

Thank you!

This was executed just as in mathematics. I should have been thinking about it :-)

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

Here is an example, that makes up a class with some features:

- it can collect images
- each image has a short name, the image file name and a message, that is printed if the image is not found
- if the image is not registered, a default message is printed
- you can restrict the search of all contained images to a region

--- the class definition:

class GuiElements(dict): # inherits from Python's dictionary
    def __init__(self):
        self.region = SCREEN # the default region
        dict.__init__(self) # super should be initialized too

    def clickElement(self, img): # the feature
        # click image if it exists and return match
        # print message otherwise and return None
        e = self.get(img, [img, img+" not found"]) # dictionary magic
        msg = e[1]
        if self.region.exists(self[e][0]):
            click(self.region.getLastMatch())
            return self.region.getLastMatch()
        else:
            print msg
            return None

    def setRegion(self, reg): # set the restricting region
        self.region = reg

    def getRegion(self): # access the current region
        return self.region

--- sample usage:

myReg = some_application_window

g = GuiElements() # get instance
g.setRegion = myReg # restrict the searches
g["Button1"] = ["image-of-button1.png", "Sorry, Button1 currently not visible!"] # register a button

# click a registered element
if not g.clickElement("Button1"): exit(1)
print "we clicked Button1", g.getRegion().getLastMatch()

# click a not registered element
img = "some-other-button.png"
if not g.clickElement(img): exit(1)
print "we clicked", img, g.getRegion().getLastMatch()

--- and now?
- you might implement more features and/or make it more robust
- you might add a feature, that adds the last match of a registered image to the container (self[img][2] = self.region.getLastMatch())
- you might add more versions of the imagefile, that reflects different system environments or screen resolutions
- ....

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

I hate, that you cannot edit at least your own comments :-((

--- sample usage:

myReg = some_application_window # has to be a valid region

g = GuiElements() # get instance
g.setRegion() = myReg # restrict the searches
g["Button1"] = ["image-of-button1.png", "Sorry, Button1 currently not visible!"] # register a button

# click a registered element
if not g.clickElement("Button1"): exit(1)
print "we clicked Button1", g.getRegion().getLastMatch()

# click a not registered element
img = "some-other-button.png"
if not g.clickElement(img): exit(1)
print "we clicked", img, g.getRegion().getLastMatch()

Revision history for this message
Calle Rundgren (c-rundgren) said :
#7

That class was pretty cool, thank you RaiMan.