I have two combo boxes. I am trying to add items dynamically into the second combobox from the first combobox's onSelChange(self, selection)
event.
due to some reason, this is not working.
Here is my sample code:
import arcpy
import pythonaddins
class ComboBoxClass1(object):
"""Implementation for TestCombo_addin.combobox (ComboBox)"""
def __init__(self):
self.items = ["cb1item1", "cb1item2"]
self.editable = True
self.enabled = True
self.dropdownWidth = 'WWWWWW'
self.width = 'WWWWWW'
self.cb2=ComboBoxClass2()
def onSelChange(self, selection):
pythonaddins.MessageBox(selection,"Message",0)
self.cb2.items.append(selection)
self.cb2.refresh()
def onEditChange(self, text):
pass
def onFocus(self, focused):
pass
def onEnter(self):
pass
def refresh(self):
pass
class ComboBoxClass2(object):
"""Implementation for TestCombo_addin.combobox_1 (ComboBox)"""
def __init__(self):
self.items = ["cb2item1", "cb2item2"]
self.editable = True
self.enabled = True
self.dropdownWidth = 'WWWWWW'
self.width = 'WWWWWW'
def onSelChange(self, selection):
pass
def onEditChange(self, text):
pass
def onFocus(self, focused):
pass
def onEnter(self):
pass
def refresh(self):
pass
Best Answer
I think setting a hook to a class instance to be accessed through a class property (as opposed to a property of an instance of that class) is bad practice and while it works fine here will cause you grief if you use this construct in other code.
E.g. If you write a class and create more than one instance of it, the hook property of the class will get overwritten each time an instance is created.
What I do is use the instances of the classes, not the classes themselves.
For example, in
config.xml
you will have something like:Note the
id
andrefID
are the same. These will refer to instances of these classes.Then you just replace references to
ComboBoxClass2
tocombobox2
in your code:If you run into a
NameError
(an issue with earlier ArcGIS versions, not sure if still a problem), put the following at the bottom of ComboBox_addin.py: :