Adding Combobox/Dropdown List to Python Toolbox Tool Dialog?

8112
5
12-12-2017 02:20 PM
BehrouzHosseini
Regular Contributor

I am trying to create a Python Toolbox tool which is supposed to add two fields State and StateAbb to selected Shapefile from selected combobox.

How I can add a Combobox which contains a list of US states to the Tool?

import arcpy

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "states"
        self.alias = "states"

        # List of tool classes associated with this toolbox
        self.tools = [states]


class states(object):
    def __init__(self):
        self.label = "states Tool"
        self.description = "Add and Insert Value from Combo"
        self.canRunInBackground = False

    def getParameterInfo(self):

        param0 = arcpy.Parameter(
            displayName="1- State Street Layer",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Optional",
            direction="Input"
        )

        params = [param0]

        return params


    def execute(self, params, messages):
        """The source code of the tool."""
        arcpy.AddField_management(params[0],"area","TEXT","#","#","#","#","NULLABLE","NON_REQUIRED","#")
        arcpy.CalculateField_management(params[0],"area","!shape.area@squaremeters!","PYTHON_10.3","#")


        return
0 Kudos
5 Replies
JoeBorgione
MVP Emeritus

I'm not sure you can add a combo box to an arcpy tool; I kind of think comboboxes are a pythonadding thing.  All I see when I google arcgis and combobox are references to the addin...

Creating a Python Add-In Combo Box—Help | ArcGIS Desktop 

One of these folks may know for sure:

Dan Patterson

Rebecca Strauch, GISP

bixb0012

That should just about do it....
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I pretty much create tools from scripts, create a tool in my toolbox, and use the Parameter setup in for the tool to create combo boxes.   I just created one today for one of my tools (for creating some mosaic data bases). 

you can set a default if you want...I didn't want one.  My list of values:

Then accessing in my script....

import arcpy
import sys

def myMsgs(message):
     arcpy.AddMessage("{0}".format(message))
     print("{0}".format(message)  )


typeList = arcpy.GetParameterAsText(6)
myMsgs("  - the typeList {0}".format(typeList))
if not typeList:
     #mdList = ["rgb", "DEMEllipsoidal"] #, "DEMEEllipsoidal",  "DEMOrthometric", "pan", "cir"]
     #mdList = ["cir", "DEMEllipsoidal",  "DEMOrthometric"]
     myMsgs("Can run manually by modifying list and uncommenting one of the above.")
     sys.exit()
else:
     mdList = typeList.split(";")

myMsgs("  --- the mdList is {0}\n".format(mdList))
#sys.exit()
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Sorry this is a bit messy.  This is a work in progress and just added that in (and was testing) for the last item on the list.  But wanted to make it available for other in my org in the future. 

Note sure if this will help or not, but it's there. 

(Funny, I couldn't get this to work last week and it was easy today.....must have been the cold med fuzziness)

edit...forgot to add a pic of the UI

0 Kudos
DonMorrison1
Frequent Contributor

Hi Rebecca - I know this is a bit dated and sorry to dredge it back up, but I'm trying to figure out how to add a combo box to a python tool.  My understanding is that a combo box combines an editable field with a drop down list - so the user is not bound to entering just the fields in the drop down list (this is what I want).  Your image looks more like a multi-select input field. Do you know if a combo box (what I'm seeking) is possible? Thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

a listbox serves the same purpose as a combobox in that they have the same listeners and event scripts (or they used to).  A combobox only served a purpose of taking up less real estate on the dialog especially if you had a longish list of entries to fill.  Unfortunately the dialog interface isn't documented anywhere... for all we know it could be the Dialog Designer in disguise

0 Kudos
JoeBorgione
MVP Emeritus

Bengi Geoca‌ - as you can see there is wealth of knowledge here. Being a certified googler, I tried a slightly different search after reading Rebecca Strauch, GISP‌ post.  I found this:

arcpy - Choosing string from dropdown list rather than typing it into Python script tool? - Geograph... 

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