Select to view content in your preferred language

Python Addin ComboBox, Passing Selection into Definition Query

4220
6
08-21-2012 10:39 AM
MikeMacRae
Frequent Contributor
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
Tags (2)
0 Kudos
6 Replies
JustinShepard
Deactivated User
Does your selection need to be inside a set of quotes? Can you send the selection to a MessageBox so that we can see what exactly is getting passed?
0 Kudos
MikeMacRae
Frequent Contributor
Hey Justin, thanks for the response.

I tried to place the selection in quotes like:
lyr.definitionQuery = '"SITE" = selection'

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

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


but it either crashes the combo box or just adds the 'selection' variable as a text string.

I'm thinking the selection in
def onSelChange(self, selection):
is the variable used to store the selection the user makes from the combo box (I could be wrong, this is my first python combo box) I thought it would be easy just to pass that into the definition query. I even tried to convert it to string
lyr.definitionQuery = '"SITE" =' str(selection)
but no luck. Obviously I'm mistaking the functionality of that variable....
0 Kudos
JustinShepard
Deactivated User
we might be overthinking this. There's a + missing in appending your strings.
Example is:
strQPrefix = "\"Named\" ="
strSelection = "'Snow, Peak Mt.'"
strDFQuery = strQPrefix + strSelection
print strDFQuery
layerTarget.definitionQuery = strDFQuery

* the \" is an escape so that python treats the " following the slash as a character and not an indication of a string start or finish

* the strSelection = "'Snow, Peak Mt.'" would get changed for you to something more like strSelection = "'" + selection + "'"

* instead of print try pythonaddins.MessageBox(strDFQuery)
0 Kudos
MikeMacRae
Frequent Contributor
You're right Justin. This was simpler than I made it out to be. We both came to the same conclusion at the same time. This is what worked for me:

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


The crashing of the addin with some of my other approaches made me think the selection variable was returning in some weird format which took me in a huge tangent.

Cheers,
Mike
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
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


Please mike i am trying to get something very close to what you've posted - i have a field called Name_Allottee and i also want to create a list from this attributes and pass it into my combo box and further drill down with a layer definition query, this is my code can you help me please? Thanks

import arcpy
import pythonaddins


   
class SelectPlotComboBoxClass(object):
    """Implementation for SelectPlot_addin.combobox (ComboBox)"""
    def __init__(self):
        #self.items = ["item1", "item2"]
        Name_Allottee = []
        for row in arcpy.SearchCursor("KWAGIS_Parcels"):
            Name.append(row.OBJECTID)
        self.items = sorted(Name_Allottee)
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
    def onSelChange(self, selection):
        
        mxd = arcpy.mapping.MapDocument("CURRENT")

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

        Name_Allottee = []
        for row in arcpy.SearchCursor("KWAGIS_Parcels"):
            Name_Allottee.append(row.OBJECTID)

        for lyr in arcpy.mapping.ListLayers(mxd, "", Layers):
            if lyr.name == "KWAGIS_Parcels":
                lyr.definitionQuery = '"Name_Allottee"' + "=" + "'" + selection + "'"

        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()


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


I also got a big red mark when i tried to install the addin.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
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


Please mike i am trying to get something very close to what you've posted - i have a field called Name_Allottee and i also want to create a list from this attributes and pass it into my combo box and further drill down with a layer definition query, this is my code can you help me please? Thanks
import arcpy
import pythonaddins


   
class SelectPlotComboBoxClass(object):
    """Implementation for SelectPlot_addin.combobox (ComboBox)"""
    def __init__(self):
        #self.items = ["item1", "item2"]
        Name_Allottee = []
        for row in arcpy.SearchCursor("KWAGIS_Parcels"):
            Name.append(row.OBJECTID)
        self.items = sorted(Name_Allottee)
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
    def onSelChange(self, selection):
        
        mxd = arcpy.mapping.MapDocument("CURRENT")

        for df in arcpy.mapping.ListDataFrames(mxd):
            if df.name == "Layers":
                dfmain = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
           
        Name_Allottee = []
        for row in arcpy.SearchCursor("KWAGIS_Parcels"):
            Name_Allottee.append(row.OBJECTID)

        for lyr in arcpy.mapping.ListLayers(mxd, "", Layers):
            if lyr.name == "KWAGIS_Parcels":
                lyr.definitionQuery = '"Name_Allottee"' + "=" + "'" + selection + "'"

        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()


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


I also got a big red mark when i tried to install the addin.
0 Kudos