BrokenLegMike

Python Addin ComboBox, Passing Selection into Definition Query

Discussion created by BrokenLegMike on Aug 21, 2012
Latest reply on Aug 26, 2012 by GISGURU_001
I am trying to pass the users selection into a definition query in an addin combo box python script.

Basically, I have a feature class with a field called "SITE". I build a python list from that field and pass it into the combo box items list. The user will choose a value in the combo box box from that list. I want to further pass the users selection into a definition query.

You can see in my script below, I just tried to pass the selection into the definition query, but it crashes the whole combo box and toolbar (i.e. when I reinstall my addin, the tool shows up on my toolbar in big red letters "MISSING"). Obviously I am doing something wrong it I believe it's how I'm passing the selection. As suggestions?

import arcpy
import pythonaddins

sitename = []
for row in arcpy.SearchCursor("Z:\JACOS\Data\GIS\Geodatabases\JACOS_MASTER_2011_PDA_CR.gdb\IF_PDABNDY_R"):
    sitename.append(row.SITE)
   
class ComboBoxClass(object):
    """Implementation for SolsticeFigureUpdater_addin.combobox (ComboBox)"""
    def __init__(self):
        #self.items = ["item1", "item2"]
        self.items = sorted(sitename)
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWWWW'
        self.width = 'WWWWWWWW'
    def onSelChange(self, selection):
        mxd = arcpy.mapping.MapDocument("CURRENT")

        for df in arcpy.mapping.ListDataFrames(mxd):
            if df.name == "DFMAIN":
                dfmain = arcpy.mapping.ListDataFrames(mxd, "DFMAIN")[0]
            if df.name == "DFINSET":
                dfinset = arcpy.mapping.ListDataFrames(mxd, "DFINSET")[0]

        for lyr in arcpy.mapping.ListLayers(mxd, "", dfmain):
            if lyr.name == "PDA Boundary":
                lyr.definitionQuery = '"SITE" =' selection

        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()


    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass

Outcomes