Creating a Python Add-In Combo Box

Creating an add-in combo box

A combo box contains an editable field and a drop-down list. The user can select a value from the drop-down list, which appears at the user's request. If you make the combo box editable, the combo box will include an editable field into which the user can type a value. The scale combo box in ArcMap is a good example of a combo box. When data with a known coordinate system is added to ArcMap, the scale combo box is enabled, giving you a set of predefined scales from which to choose. It also allows you to type a desired scale not present in the list of options, and the map is displayed based on the value added.

This topic guides you through the process of creating a combo box on a new toolbar using the Python Add-In Wizard. Before beginning this workflow, make sure that you have created an ArcMap Add-In project and specified the project settings. For more information, see Creating an Add-In Project. While this workflow shows you how to create a combo box for ArcMap, you can use this process to add a combo box to any ArcGIS for Desktop application. This topic examines the process of creating a combo box with the list of layers from the table of contents. Selecting a layer will create a fishnet covering the full extent of the layer. The Python class created by the Add-In wizard is then examined in greater detail to explore the properties and methods that provide functionality to your combo box.

Creating an Add-In combo box consists of the following steps:

Steps:
  1. Create a toolbar .

    Once you have entered the required project settings, click the Add-In Contents tab and begin declaring your Add-In customizations. A combo box may reside on a toolbar but cannot exist on a menu. See Creating an Add-In Toolbar for steps on creating a toolbar.

  2. Create the combo box.

    Once you have entered the toolbar properties, you can create a new combo box. Right-click the new toolbar and select New Combo Box from the context menu.

    Setting combo box properties in Add-In Wizard

    A combo box has a number of properties for you to set. These properties are stored in the config.xml file for the project. The following is a list of all the properties with an explanation for each.

    Property

    Description

    Caption (required)

    Defines the combo box caption. The following illustration shows the caption used in the ArcGIS Add-In Manager to help identify the different types of customizations available (the type of the add-in is identified in parentheses):

    Combo box properties shown in Add-In Manager

    Class (required)

    The Python class is where you write your business logic for the combo box. This class is important because it is executed whenever the combo box is edited or the selection changes. Use the Python naming convention when constructing your class. Python classes use cap-word notation. In this example, a class named "LayersComboBox" in created.

    ID(required)

    The unique name that is used to identify your combo box. It is possible for you to create more than one combo box for a given project; this ID is used to distinguish between the different combo boxes. Ideally, you should replace the default ID with a more meaningful value. The ID should not contain any spaces. You may use underscores to connect words. You should not use Python keywords. Consult the Python documentation for reserved words.

    Tooltip (optional)

    A brief description that appears when the mouse pointer pauses over the combo box in a desktop application.

    Message (optional)

    A detailed description of what the combo box does. The message appears beneath the tooltip when the mouse pointer pauses over the combo box.

    Hint Text (optional)

    The text shown on the combo box when it is not active in the desktop application. This text can be used to educate the user about the types of items they select from or about the kind of items they need to select from the combo box.

    The final section of the combo box is the help heading and content. These properties allow you to supply information that will be used when a user invokes context-sensitive help. These are pop-up topics that remain on-screen until a user clicks somewhere else.

    The following properties compose this context-sensitive Help section:

    Property

    Description

    Heading (optional)

    Indicates what the help content is about

    Content (optional)

    The help content for the combo box

    Once you have finished entering the properties, you can click the Save button at the bottom of the wizard. This will create all the necessary files and folders within your working folder.

  3. Editing the Python script.

    At this stage, you have finished adding values for the properties needed to define the Add-In combo box. The next step in this workflow is to edit the Python script and update the Python class to include the functionality for creating a fishnet of polygons. The combo box will be populated with the list of layers from the active data frame. When a layer is selected from the combo box, a fishnet is created using the extent of the layer with a size set to 10 rows by 10 columns. To add the functionality to the combo box, perform the following steps:

    1. Edit the Python script in the Install folder located in the working folder you created through the wizard.

      A class will exist with the same name as entered through the wizard. You should not rename this class, as the class name is referenced in the config.xml. The class consists of multiple functions and properties described in Combo box Class topic.

    2. Update the functions as shown below.

      This code provides the functionality to create a fishnet of polygons with 10 rows and 10 columns using the extent of the layer selected from the combo box.

      # Business logic to implement the ComboBox
      def __init__(self):
              self.editable = True
              self.enabled = True
      
      def onSelChange(self, selection):
      
      	# When a new layer is selected, create a new fishnet using the extent of layer.
      	layer = arcpy.mapping.ListLayers(self.mxd, selection)[0]
      	desc = arcpy.Describe(layer.dataSource)
      	extent = desc.Extent
      	fishnet = arcpy.CreateFishnet_management(r'in_memory\fishnet',
      	                '%f %f' %(extent.XMin, extent.YMin),
      	                '%f %f' %(extent.XMin, extent.YMax),
      	                0, 0, 10, 10,
      	                '%f %f' %(extent.XMax, extent.YMax),
      	                'NO_LABELS',
      	                '%f %f %f %f' %(extent.XMin, extent.YMin, extent.XMax, extent.YMax), 'POLYGON')
      	arcpy.RefreshActiveView()
      
      def onFocus(self, focused):
      
        # When the combo box has focus, update the combo box with the list of layer names.
      	if focused:
      		self.mxd = arcpy.mapping.MapDocument('current')
      		layers = arcpy.mapping.ListLayers(self.mxd)
      		self.items = []
      		for layer in layers:
      			self.items.append(layer.name)
      
    3. Save the script.
  4. Testing the combo box.

    Once the combo box is created and the script code is added, it is important to make the add-in file and test the add-in before sharing it. For these steps, see Testing an Add-In.

  5. Sharing the add-in .

    After testing the combo box and confirming that it works as expected, you are ready to deploy and share the add-in. For more information, see Sharing Add-Ins.

Related Topics

10/29/2012