Getting results from your Python Add-in buttons

The new Python Add-ins for ArcGIS 10.1 bring a whole new opportunity for customizations.  The pythonaddins module provides additional functionality within add-ins that is not available in stand-alone scripts or geoprocessing script tools.  Three of these functions that allow user input or interaction are: OpenDialog, SaveDialog, and MessageBox.

Each of these functions allow information to be collected from the user.  It is up to the programmer to determine what to do with that information.

The OpenDialog and Save Dialog functions return two different values depending on how the user interacts with the dialog.  When the user selects a dataset and clicks either Open or Save, it returns the full path of the dataset chosen.  If the Cancel button is clicked it returns ‘None’.

Value being passed to a print statement when clicked.

The MessageBox function works in a similar way; however, it returns the string value representing the message button pressed.  For example, when the Ignore button is clicked it returns ‘Ignore’.

Below is a sample python add-in with comparison between the code and the dialogs:

A) The Open Dialog opens.
B) A file is chosen, and the Open button is clicked.  The file is added to the map.
C) The Cancel button is clicked which opens the Warning Message.
D) The Retry button is clicked which reopens the Open Dialog.
E) The Cancel button is clicked which opens a final Warning Message.

The above sample add-in is available on ArcGIS.com: Dialog Windows and Messages

Timothy H. – Senior Support Analyst

This entry was posted in ArcGIS for Desktop and tagged , , , , . Bookmark the permalink.

Leave a Reply

2 Comments

  1. timmo1982 says:

    Great tips- Thanks very much for these as, at the time of writing, the knowledge base around AddinToolbars is still quite sparse.
    I have another question if you don’t mind- Is it possible to add some sort of ‘wait’ instruction when calling a tool within a toolbox? For example, I have a button on a addin toolbar which calls a GPS/GPX conversion tool. I’ve also added a message box after that to ask if the user would like to open the DNR GPS program as that is the logical next step for them- Ideally I would like to wait until the toolbox process is finished before I ask that question (currently it opens the message box as soon as the toolbox tool is opened)
    Cheers

  2. Timothy says:

    On first whim without doing any research I would use the ArcPy Exists function to see if the output of the tool has been created. You could create a loop that would check to see if it exists. If it does then launch the message, and if it does not then use time.sleep to wait before trying the exists function again.

    Exists (ArcPy)
    http://resources.arcgis.com/en/help/main/10.1/index.html#//018v0000004p000000

    Time (Python)
    http://docs.python.org/2/library/time.html