Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 (using the search cursor and appending the results) 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've tried converting it to string, placing quotes around it, etc and no luck. Obviously I am doing something wrong with. I believe it's how I'm passing the selection. As suggestions?

import arcpy
import pythonaddins

sitename = []
for row in arcpy.SearchCursor("Z:\Geodatabases\Test.gdb\Tesfc"):

class ComboBoxClass(object):
    """Implementation for SolsticeFigureUpdater_addin.combobox (ComboBox)"""
    def __init__(self):
        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 == "DFMAIN":
                main = arcpy.mapping.ListDataFrames(mxd, "DFMAIN")[0]
            if == "DFINSET":
                dfinset = arcpy.mapping.ListDataFrames(mxd, "DFINSET")[0]

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


    def onEditChange(self, text):
    def onFocus(self, focused):
    def onEnter(self):
    def refresh(self):
share|improve this question
Very cool. Exactly what I have been trying to do. – dchaboya Feb 21 '13 at 19:08
up vote 1 down vote accepted

I nailed down my issue. I had to wrapt the selection in quotes, but I had to do it using concatenation in the definition query:

lyr.definitionQuery = '"SITE"' + "=" + "'" + selection + "'"

Cheers, Mike

share|improve this answer
FYI, this can be simplified to lyr.definitionQuery = '"SITE" =' + "'" + selection + "'" or even lyr.definitionQuery = '"SITE" = \'' + selection + "'" if you don't mind the escaped single-quote (\'). – nmpeterson Jan 18 '13 at 17:51
How would you set up the cursor to only return unique values? – dchaboya Feb 21 '13 at 19:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.