The pythonaddinsmodule includes functions for supporting Python add-ins.
| Function | Explanation | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
OpenDialog({title}, {multiple_selection}, {starting_location}, {button_caption}, {filter}, {filter_label})  | Opens a dialog box to choose one or more GIS datasets. This function returns the full path of the dataset chosen. If multiple datasets are chosen, it returns a list of full paths. There is no filtering of the input datasets (for example, filter only for point feature classes). 
  | ||||||||||||
SaveDialog({title}, {name_text}, {starting_location}, {filter}, {filter_label})  | Opens a dialog box to save data. This function returns the full path for the dataset to be saved. 
  | ||||||||||||
GPToolDialog(toolbox, tool_name)  | Opens a geoprocessing tool dialog box. 
  | ||||||||||||
MessageBox(message, title, {mb_type})  | Shows a message box. This function returns a string value representing the message button pressed. 
  | ||||||||||||
GetSelectedTOCLayerOrDataFrame()  | Returns the selected layer or data frame from the table of contents.  | ||||||||||||
GetSelectedCatalogWindowPath()  | Returns the full path of the selected item in the Catalog window.  | ||||||||||||
ProgressDialog()  | Returns a ProgressDialog object. The Progress dialog object becomes visible automatically when you enter the with block and disappears when you leave it. See a sample code snippet below. The properties are: 
  | 
| mb_type code | Message Box Type | 
|---|---|
0  | OK only  | 
1  | OK/Cancel  | 
2  | Abort/Retry/Ignore  | 
3  | Yes/No/Cancel  | 
4  | Yes/No  | 
5  | Retry/Cancel  | 
6  | Cancel/Try Again/Continue  | 
This add-in button uses OpenDialog() to select a set of layer files and adds each layer to the selected data frame.
import arcpy
import pythonaddins
class AddLayers(object):
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        layer_files = pythonaddins.OpenDialog('Select Layers', True, r'C:\GISData', 'Add')
        mxd = arcpy.mapping.MapDocument('current')
        df = pythonaddins.GetSelectedTOCLayerOrDataFrame()
        if not isinstance(df, arcpy.mapping.Layer):
            for layer_file in layer_files:
                layer = arcpy.mapping.Layer(layer_file)
                arcpy.mapping.AddLayer(df, layer)
        else:
            pythonaddins.MessageBox('Select a data frame', 'INFO', 0)
This add-in button opens a geoprocessing tool.
import arcpy
import pythonaddins
class OpenGPTool(object):
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pythonaddins.GPToolDialog(r'C:\MyTools\WaterStudy.tbx', 'GroundWaterYield')
Progress Dialog example: Test this in the Python window within ArcGIS for Desktop.
import pythonaddins
with pythonaddins.ProgressDialog as dialog:
    dialog.title = "Progress Dialog"
    dialog.description = "Copying a large feature class."
    dialog.animation = "File"
    for i in xrange(100):
        dialog.progress = i
        time.sleep(0.125)
        if dialog.cancelled:
            raise Exception("Ooops")
Only permit text files to be opened.
import os
class MyValidator(object):
    def __str__(self):
        return "Text files(*.txt)"
    def __call__(self, filename):
        if os.path.isfile(filename) and filename.lower().endswith(".txt"):
            return True
        return False
filename = pythonaddins.OpenDialog(r"c:\files", filter=MyValidator())