How to pass combobox's value to an button event  on a python addin toolbar

6077
2
12-13-2012 06:36 AM
X_K_YANG
New Contributor II
Dear all,
I build a python addin toolbar with a combobox and serveral buttions.
The combobox is used to display the names of featurelayer loaded in ArcGIS Map.
One button is used to check if all the values in the selected layer  in the combobox are okay or not.
So, firstly, I need list all the layers and select a layer in combobox; I've finished the part. The combobox can list all the featurelayers and after selection it can also display the layer's name.
However, I stuck at here because I don't know how pass the layer's name to that button so that when click that button the button knows which layer should be processed.
Can you kindly give any advices? Thanks for taking time to view.
Tags (2)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Xiankun,

You will want to create a global variable and then pass that to the Button class.  Here is an example:

import arcpy
import pythonaddins

class ComboBoxClass(object):
    """Implementation for ComboBox_addin.combobox (ComboBox)"""
    def __init__(self):
            self.editable = True
            self.enabled = True
            self.width = 'WWWWWWWW'
            self.dropdownWidth = 'WWWWWWW'

    def onFocus(self, focused):
        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)

    def onSelChange(self, selection):
        global fc
        fc = arcpy.mapping.ListLayers(self.mxd, selection)[0]
        
class ButtonClass(object):
    """Implementation for ComboBox_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        print fc


On the 'onSelChange' function within the ComboBoxClass I declared 'fc' as a global variable, so now I can use this in the ButtonClass.  The above code simply prints the selected layer in the combo box to the python window in ArcMap when the button is clicked.
0 Kudos
X_K_YANG
New Contributor II
Hi Xiankun,

You will want to create a global variable and then pass that to the Button class.  Here is an example:

import arcpy
import pythonaddins

class ComboBoxClass(object):
    """Implementation for ComboBox_addin.combobox (ComboBox)"""
    def __init__(self):
            self.editable = True
            self.enabled = True
            self.width = 'WWWWWWWW'
            self.dropdownWidth = 'WWWWWWW'

    def onFocus(self, focused):
        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)

    def onSelChange(self, selection):
        global fc
        fc = arcpy.mapping.ListLayers(self.mxd, selection)[0]
        
class ButtonClass(object):
    """Implementation for ComboBox_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        print fc


On the 'onSelChange' function within the ComboBoxClass I declared 'fc' as a global variable, so now I can use this in the ButtonClass.  The above code simply prints the selected layer in the combo box to the python window in ArcMap when the button is clicked.


Hi Jskinn3,
Thank you very much. That is exactly what I need.
0 Kudos