The pythonaddins module

The pythonaddins module includes functions for supporting Python add-ins.

NoteNote:

The pythonaddins module can only be used within a Python add-in. It cannot be used in stand-alone scripts or geoprocessing script tools.

Function

Explanation

OpenDialog({title}, {multiple_selection}, {starting_location}, {button_caption})

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

  • {title}—The dialog box's title.
  • {multiple_selection}—Indicates if multiple items may be selected. False, by default.
  • {starting_location}—The path to the starting location.
  • {button_caption}—The caption to use for the Open button.

SaveDialog({title}, {name_text}, {starting_location})

Opens a dialog box to save data. This function returns the full path for the dataset to be saved.

  • {title}—The dialog box's title.
  • {name_text}—The dataset name displayed in the name text box on the dialog box.
  • {starting_location}—The path to the starting location where the data will be saved.

GPToolDialog(toolbox, tool_name)

Opens a geoprocessing tool dialog box.

  • toolbox—The toolbox location.
  • tool_name—The tool name.

MessageBox(message, title, {mb_type})

Shows a message box. This function returns a string value representing the message button pressed.

  • message—The message to be displayed.
  • title—The message box title.
  • {mb_type}—The type of message box to display. The default option is 0 (OK message). For a complete list of {mb_type} codes, see the table below.

GetSelectedTOCLayerOrDataFrame()

Returns the selected layer or data frame from the table of contents.

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

{mb_type} codes

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')

10/29/2012