10.2 Arcpy comboboxclass addin simple question

3158
10
Jump to solution
03-12-2014 11:04 AM
jasongraham
New Contributor III
Hopefully this is a simple question,  what variable is used to get the selection from this combo box?  ie how do I use this in later code.  I am not finding any good resources for arcpy addins.

class ComboBoxClass4(object):     """Implementation for MTR_addin.combobox (ComboBox)"""     def __init__(self):         self.value = "S"           self.items = ("C", "F", "K", "S", "U")          self.editable = True         self.enabled = True         self.dropdownWidth = 'WWW'         self.width = 'WWW'     def onSelChange(self, selection):         pass     def onEditChange(self, text):         pass     def onFocus(self, focused):         pass     def onEnter(self):         pass     def refresh(self):         pass
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
And remember when you access your combobox you need to first have assign it properties through the class level self variables. You might be missing this part in your combobox class.

    def onSelChange(self, selection):         self.sel = selection

Then get the selection in your other classes like this.
combobox.sel

View solution in original post

0 Kudos
10 Replies
MathewCoyle
Frequent Contributor
You need to use the ID from the addin assistant. In your case it is called 'combobox'
0 Kudos
jasongraham
New Contributor III
You need to use the ID from the addin assistant. In your case it is called 'combobox'


Nope, says combobox not defined.  Do you know of any better resources for arcpy addin, the python is different from scrpting
0 Kudos
MathewCoyle
Frequent Contributor
Then you probably changed it manually. Check in the addin assistant what the ID is for your combo box class. All you need are in the help docs.

http://resources.arcgis.com/en/help/main/10.2/index.html#/combo_box/014p00000028000000/
0 Kudos
jasongraham
New Contributor III
Then you probably changed it manually. Check in the addin assistant what the ID is for your combo box class. All you need are in the help docs.

http://resources.arcgis.com/en/help/main/10.2/index.html#/combo_box/014p00000028000000/



 - <Toolbar caption="MTR Select" category="Township Select" id="MTR_addin.toolbar" showInitially="true">
- <Items>
  <Button refID="MTR_addin.button" /> 
  <ComboBox refID="MTR_addin.combobox" /> 
  <ComboBox refID="MTR_addin.combobox_1" /> 
  <ComboBox refID="MTR_addin.combobox_2" /> 
  <ComboBox refID="MTR_addin.combobox_3" /> 
  <ComboBox refID="MTR_addin.combobox_4" /> 
  </Items>
  </Toolbar>
  </Toolbars>


I did not change it, it is combobox, but if I try 'print combobox' I get the error ' combobox not defined
0 Kudos
MathewCoyle
Frequent Contributor
print combobox? Are you trying to run this in an IDE? That won't work for addins. You'll only be able to reference the class values via the ID when it is installed as an addin. What is your end goal for this addin?
0 Kudos
MathewCoyle
Frequent Contributor
And remember when you access your combobox you need to first have assign it properties through the class level self variables. You might be missing this part in your combobox class.

    def onSelChange(self, selection):         self.sel = selection

Then get the selection in your other classes like this.
combobox.sel
0 Kudos
jasongraham
New Contributor III
print combobox? Are you trying to run this in an IDE? That won't work for addins. You'll only be able to reference the class values via the ID when it is installed as an addin. What is your end goal for this addin?



ok, here is the whole code.  The original code works as a script but I decided to try making an addin tool.
I am not a great coder, trying to learn.  This tool references a township and range layer, with the comboboxs the user can select meridain, township, north/south, range, east/west.  much of the code works to populate the pull down from the attributes with unique values but doesnt seem to sort.  I am able to pass the values for my Twp and Rng but not the M, NS, EW that have a list of values, I could populate like the other two but it seems to bog down,  suggestions?( no longer a simple question i guess)  alot of stuff on the bottom blocked out that I am working on.

# ---------------------------------------------------------------------------
# Township Selector
# Created on: 2014-02-24 15:28:23.00000
# Create by Jason Graham
# Usage: <NS> <EW> <Rng> <Twp> <M> <MTR> <MTRsel>
# Description:Add-in tool to easily select a township then zoom to the selection 
# ---------------------------------------------------------------------------
import arcpy
import pythonaddins
from arcpy import env

 


env.workspace = r"X:\JasonGraham\Landstatus\Data\ls2014.gdb\BaseData"

mxd = arcpy.mapping.MapDocument("CURRENT")

class ComboBoxClass4(object):
    """Implementation for MTR_addin.combobox (ComboBox)"""
    def __init__(self):
        self.value = "S"  
        self.items = ("C", "F", "K", "S", "U") 
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWW'
        self.width = 'WWW'
    def onSelChange(self, selection):
        pass
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass


Twp = []
for row in arcpy.SearchCursor("X:\JasonGraham\Landstatus\Data\ls2014.gdb\BaseData\Townships"):
    Twp.append(row.TWP_NUMC)

class ComboBoxClass5(object):
    """Implementation for MTR_addin.combobox_1 (ComboBox)"""
    def __init__(self):
        self.value = "010"
        self.items = sorted(Twp)
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWW'
        self.width = 'WWWW'
    def onSelChange(self, selection):
        global valName
        val = (self.items, selection)[1]
        valName = str(val)
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):#show only unique values
        if focused:
            values = sorted(Twp)
            uniqueValues = set(values)
            self.items = []
            for uniqueValue in uniqueValues:
                self.items.append(uniqueValue)
    def onEnter(self):
        pass
    def refresh(self):
        pass


class ComboBoxClass6(object):
    """Implementation for MTR_addin.combobox_2 (ComboBox)"""
    def __init__(self):
        self.value = "N"
        self.items = ("N", "S")
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWW'
        self.width = 'WWW'
    def onSelChange(self, selection):
        pass
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass
 

Rng = []
for row in arcpy.SearchCursor("X:\JasonGraham\Landstatus\Data\ls2014.gdb\BaseData\Townships"):
    Rng.append(row.RNG_NUMC)
    
class ComboBoxClass7(object):
    """Implementation for MTR_addin.combobox_3 (ComboBox)"""
    def __init__(self):
        self.value = "010"
        self.items = sorted(Rng)
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWW'
        self.width = 'WWWW'
    def onSelChange(self, selection):
        global valName
        val = (self.items, selection)[1]
        valName = str(val)
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):#show only unique values
        if focused:
            values = sorted(Rng)
            uniqueValues = set(values)
            self.items = []
            for uniqueValue in uniqueValues:
                self.items.append(uniqueValue)
    def onEnter(self):
        pass
    def refresh(self):
        pass
    
class ComboBoxClass8(object):# Collect East/west   
    def __init__(self):
        self.value = "E"
        self.items = ("E", "W")
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWW'
        self.width = 'WWW'
    def onSelChange(self, selection):
        selection       
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass               
    def onEnter(self):
        pass
    def refresh(self):
        self.refresh()
    

#concatenate inputs
#MTRsel = combobox + Twp + combobox_2() + Rng(selected) + combobox_4()
#print ComboBoxClass8(self)


class ButtonClass3(object):
    """Implementation for MTR_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pass       
        
        #arcpy.SelectLayerByAttribute_management("Townships","NEW_SELECTION",'"MTR" =' + "'%s'" %MTRsel)
        #arcpy.mapping.ListDataFrames(mxd)[0].zoomToSelectedFeatures()  
        return None
# Select user input value
#arcpy.SelectLayerByAttribute_management("Townships","NEW_SELECTION",'"MTR" =' + "'%s'" %MTRsel) #gets error "ExecuteError: ERROR 000358: Invalid expression
#arcpy.SelectLayerByAttribute_management("Townships","NEW_SELECTION",'"MTR" =' +  MTRsel) gets error "TypeError: cannot concatenate 'str' and 'list' objects"
#arcpy.SelectLayerByAttribute_management("Townships","NEW_SELECTION",MTRsel) gets error RuntimeError: Object: Error in executing tool

#Zoom to selection
#arcpy.mapping.ListDataFrames(mxd)[0].zoomToSelectedFeatures()   
0 Kudos
MathewCoyle
Frequent Contributor
Yes that adds a significant layer of complexity. What I would recommend is getting one combobox to work as you want, ignoring the rest. Once you know exactly how a combobox works you can create the rest fairly easily. I would recommend adding an onClick function to a button that just calls an arcpy.AddMessage() with the value of your combobox for testing.

An alternative is to add your working toolbox to your addin. See this post for details.
http://forums.arcgis.com/threads/89502-Python-add-ins-vs.-.NET-add-ins?p=319191&viewfull=1#post31919...
0 Kudos
FredKellner1
New Contributor
Comboxes 5 and 7 create global variables from your combobox selections. But they have the same name. I think this is a potential problem. Your other comboboxes do not have defined global variables related to combobox selection so this seems like a problem as well. Using the toolbar with Arc Python window open is good for testing toolbars when they are not working properly often times you we get error messages that will help you eliminate problems with your toolbar code.

If you are trying to use the comboboxes to allow the user to select Lat, Long, Twn and Rng, then create a global variable with a different name from each combobox selection and call these later when you concatenate them together.
0 Kudos