Passing ComboBox Input to Button

780
1
10-20-2017 09:41 AM
JoeBorgione
MVP Emeritus

There are number of posts on this very subject, and I've tried to mimic the successful ones when it comes to passing the contents of a ArcPython Addin Combo Box to a Button, I've not been able to get it work.

The premise:  the user enters a string, in this particular case a proposed new street name.  Once that name is entered, the value is passed to a button, which runs a search cursor upon a list of existing street names and looks for a match.

The code:

import arcpy
import pythonaddins
global street‍‍‍


class ButtonClass2(object):
    """Implementation for CheckStreetName_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        #streetname = "AIR BASE VILLAGE" this was hard coded in for testing purposes
        with arcpy.da.SearchCursor("FullNameOnly","FullName") as cursor:
            for row in cursor:
                fromlist = row[0]
                if "'{}'".format(fromlist) == "'{}'".format(global street):
                    search = "yes"
                    break
                elif  "'{}'".format(fromlist) != "'{}'".format(global street):
                    search = "no"
        if search == "yes": pythonaddins.MessageBox('The Name Is Being Used','StreetNameCheck',0)
        elif search == "no": pythonaddins.MessageBox('This name is available','StreetNameCheck',0)

class ComboBoxClass1(object):
    """Implementation for CheckStreetName_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        pass
    def onEditChange(self, text):
        global street = text
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass

If I run this with the hard-coded streetname variable commented out in line 12, plugging it into the appropriate locations it'll work just fine.  However, once I add global street to lines 16 and 19 in the button and then line 35 in the combo box, my addin tool box containing the combo box and button gets hosed: 

When using a combo box in this fashion, what is the acceptable means to set the value of a variable entered into that combo box?  A tab, return, etc?  Additionally, how do I pass the entered text to the button?

That should just about do it....
0 Kudos
1 Reply
JoeBorgione
MVP Emeritus

A few minutes after my post above, I think I've got it figured it out.  My reference to the global variable is the issue.  see new code:

class ButtonClass2(object):
    """Implementation for CheckStreetName_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        #streetname = "AIR BASE VILLAGE" this was hard coded in for testing purposes
        with arcpy.da.SearchCursor("FullNameOnly","FullName") as cursor:
            for row in cursor:
                fromlist = row[0]
                if "'{}'".format(fromlist) == "'{}'".format(street):
                    search = "yes"
                    break
                elif  "'{}'".format(fromlist) != "'{}'".format(street):
                    search = "no"
        if search == "yes": pythonaddins.MessageBox('The Name Is Being Used','StreetNameCheck',0)
        elif search == "no": pythonaddins.MessageBox('This name is available','StreetNameCheck',0)

class ComboBoxClass1(object):
    """Implementation for CheckStreetName_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWWW'
    def onSelChange(self, selection):
        pass
    def onEditChange(self, text):
        global street
        street = text
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass

At line 30 & 31 I made what appears to be the winning change; in lines 11 and 14 you'll see the change as well.  Now when I enter text into the combo box hit tab and then click the button, I get the expected results...

That should just about do it....
0 Kudos