English

How To: Dynamically add items into a Python add-in combo box from another add-in combo box

Summary

There can be multiple add-in combo boxes in a project and in some cases, it may be desirable to have two or more combo boxes to mirror one another. In a project, more items can be added to a combo box add-in along the process. If there are other combo boxes mirroring the currently edited combo box, it is desirable for the other combo boxes to copy the items dynamically. This is possible using the onSelChange(self selection) method.

Procedure

The following steps demonstrate how to dynamically add items into a Python add-in combo box from another add-in combo box:

  1. Import the necessary modules.
import arcpy
import pythonaddins
  1. Create the Combo Box classes. The following sample shows two combo box mirroring each other.
class cbxClass1 (object):
class cbxClass2 (object):
  1. In each of the classes, define the instances for the combo box.
def __init__(self):
        self.items = [chr(n) * 3 for n in range(65, 65+3)] # [AAA, BBB, CCC]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
        cbxClass1._hook = self
def __init__(self):
        self.items = [chr(n) * 4 for n in range(90, 90 - 3, -1)] # [ZZZ, XXX, YYY]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
        cbxClass2._hook = self
  1. Add the onSelChange(self, selection) method.
def onSelChange(self, selection):
        cbxClass2._hook.items.extend([selection])
def onSelChange(self, selection):
        cbxClass1._hook.items.extend([selection])
The following is a sample of a full code.
import arcpy
import pythonaddins

class cbxClass1(object):
    """Implementation for ComboBoxExample_addin.cbx1 (ComboBox)"""
    def __init__(self):
        self.items = [chr(n) * 3 for n in range(65, 65+3)] # [AAA, BBB, CCC]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
        cbxClass1._hook = self
    def onSelChange(self, selection):
        cbxClass2._hook.items.extend([selection])

class cbxClass2(object):
    """Implementation for ComboBoxExample_addin.cbx2 (ComboBox)"""
    def __init__(self):
        self.items = [chr(n) * 4 for n in range(90, 90 - 3, -1)] # [ZZZ, XXX, YYY]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
        cbxClass2._hook = self
    def onSelChange(self, selection):
        cbxClass1._hook.items.extend([selection])

Related Information