← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #144772]: Python style Dictionary, string as key

 

Question #144772 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/144772

RaiMan posted a new comment:
Here is an example, that makes up a class with some features inheriting
its main features from Pythons dictionary class::

- 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
- ....

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.