[GIS] Clearing Combobox in Python Addin

arcgis-10.2arcpycombo-boxpython-addin

I'm using ArcGIS 10.2.1 and have written an Python addin with multiple comboboxes and an "Unselect" button. The comboboxes run queries on the data and the button clears the selections.

I need the "Unselect" button to clear the comboboxes of their entries. I've tried both of these options:

1) Self.Editable=False, then the selection in the comboboxes disappear when the user selects something. That's what I posted this question in regards to.

2) Self.Editable=True, then the selection remains visible, but I can't get it to clear when the "Unselect" button is clicked, even when the selection is cleared. (Code below)

So, edit=true, can't get the unselect to clear the combobox, edit = false, the selection in the combobox automatically disappears.

Any other thoughts?

class ButtonUnselect(object):
"""Implementation for Walla_Walla_QueryTool_addin.button (Button)"""
def __init__(self):
    self.enabled = True
    self.checked = False
def onClick(self):
    self.mxd = arcpy.mapping.MapDocument('current')
    lyr = arcpy.mapping.ListLayers(self.mxd, "PODs")[0]
    lyr2 = arcpy.mapping.ListLayers(self.mxd, "POUs")[0]
    lyr3 = arcpy.mapping.ListLayers(self.mxd, "Management_Points")[0]
    arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
    arcpy.SelectLayerByAttribute_management(lyr2, "CLEAR_SELECTION")
    arcpy.SelectLayerByAttribute_management(lyr3, "CLEAR_SELECTION")
    df = arcpy.mapping.ListDataFrames(self.mxd)[0]
    df.extent = ext
    ext = lyr2.getExtent()
    combobox_2.__init__

class ComboBoxPurpose(object):
"""Implementation for Walla_Walla_QueryTool_addin.combobox_5 (ComboBox)"""
def __init__(self):
    self.editable = True
    self.enabled = True
    self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
    self.width = 'WWWWWWWWWWWWWWWWWW'
def onSelChange(self, selection):
    self.mxd = arcpy.mapping.MapDocument('current')
    layer1 = arcpy.mapping.ListLayers(self.mxd, "PODs")[0]      
    layer2 = arcpy.mapping.ListLayers(self.mxd, "POUs")[0]
    arcpy.SelectLayerByAttribute_management(layer1, "NEW_SELECTION", "Purpose = '" + selection + "'")
    arcpy.SelectLayerByAttribute_management(layer2, "NEW_SELECTION", "Purpose = '" + selection + "'")
    arcpy.RefreshActiveView()
    df = arcpy.mapping.ListDataFrames(self.mxd)[0]
    layer1.getSelectedExtent()
    layer2.getSelectedExtent()
    df.zoomToSelectedFeatures()
def onEditChange(self, text):
    pass
def onFocus(self, focused):
    self.mxd = arcpy.mapping.MapDocument('current')
    layer3 = arcpy.mapping.ListLayers(self.mxd, "PODs")[0]
    self.items = []
    values = [row[0] for row in arcpy.da.SearchCursor(layer3, ["Purpose"])]
    for uniqueVal in sorted(set(values)):
                self.items.append(uniqueVal)
def onEnter(self):
    pass
def refresh(self):
    pass

Best Answer

So, just to follow up on what Luke said, I created (see below) a very simple example that makes a pull-down of current map layers, which can be easily modified to list something else and act on the selection.

The workaround mentioned executes with onEnter so that the last selected val is reset in the combobox. Some comments and print statements left in for your further testing convenience.


I added code (a button class, as you specified) that 'clears' the value of the combobox. The 'refresh' function is called in the button class from the combobox class - that's an important distinction, why I chose to add to the code (refresh is called 'internally' by the combobox class as well as 'externally' by the button class).

import arcpy
import pythonaddins

class ButtonClass7(object):
    """Implementation for testNonEditCombo_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        combobox.value = ''
        combobox.refresh()

class ComboBoxClass1(object):
    """Implementation for testNonEditCombo_addin.combobox (ComboBox)"""

    def __init__(self):
        self.editable = True
        self.enabled = True
        # introducing global
        global staticVal
        self.newfunction()
        self.value = staticVal = self.items[0]

    def onSelChange(self, selection):
        # redefining global
        global staticVal
        self.value = staticVal = selection
        print 'selection: {0}'.format(selection)

    def onEditChange(self, text):
        # I toyed with this function but decided not to use it in this example.
        pass

    def onFocus(self, focused):
        # if any layers are added/removed from the TOC, the list is refreshed.
        if focused: self.newfunction()

    def onEnter(self):
        self.value = staticVal
        print 'self.refresh...so did the refresh function fire?'
        self.refresh()

    def refresh(self):
        arcpy.RefreshActiveView()
        print 'refreshed...'

    def newfunction(self):
        self.mxd = arcpy.mapping.MapDocument('current')
        self.items = [lyr.name for lyr in arcpy.mapping.ListLayers(self.mxd)]
Related Question