ArcGIS Desktop

  • ArcGIS Pro
  • ArcMap

  • My Profile
  • Help
  • Sign Out
ArcGIS Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS Desktop

A complete professional GIS

ArcGIS Enterprise

GIS in your enterprise

ArcGIS Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

ArcMap

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • Extensions

The pythonaddins module

The pythonaddinsmodule includes functions for supporting Python add-ins.

Note:

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

FunctionExplanation

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

  • {title}—The title of the dialog box.
  • {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.
  • {filter}—A callable filter argument. The callable routine takes a single argument— a catalog path to a selected item—and returns true or false depending on whether the item can be opened or not. See code example below.
  • {filter_label}—It is the text seen in the Show of type: drop-down box on the dialog box.

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.

  • {title}—The title of the dialog box.
  • {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.
  • {filter}—A callable filter argument. The callable routine accepts a single argument—a catalog path to a selected item—and returns true or false depending on whether the item can be saved or not. See code examples below.
  • {filter_label}—It is the text seen in the Show of type: drop-down box on the dialog box.

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.

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:

animation

Valid values are None, "File", "Spiral"

description

Long description

title

Dialog title

cancelled

Returns true if the cancel button is pressed

canCancel

Enables or disables the cancel button

progress

Number between 1 and 100 representing the bar's progress

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

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

ArcGIS Desktop

  • Home
  • Documentation
  • Support

ArcGIS

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS
  • ArcGIS Developer
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Esri Blog
  • User Conference
  • Developer Summit
Esri
Tell us what you think.
Copyright © 2021 Esri. | Privacy | Legal