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
Mona CoderMona Coder
asked Dec 12, 2017 at 18:08
-
can you explain what a combo-box is?NULL.Dude– NULL.Dude2017年12月12日 18:12:11 +00:00Commented Dec 12, 2017 at 18:12
-
You are using a Python Toolbox tool not a Python Script tool.PolyGeo– PolyGeo ♦2017年12月12日 19:09:50 +00:00Commented Dec 12, 2017 at 19:09
-
1@Geo.Dude en.wikipedia.org/wiki/Combo_boxuser2856– user28562017年12月13日 02:06:05 +00:00Commented Dec 13, 2017 at 2:06
1 Answer 1
Use a "Value List"
param.filter.type = "ValueList"
param.filter.list = [a, list, of, states]
E.g.
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
self.states = {'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ', 'Arkansas': 'AR',
'California': 'CA', 'Colorado': 'CO', 'Connecticut': 'CT',
'Delaware': 'DE', 'Florida': 'FL', 'Georgia': 'GA', 'Hawaii': 'HI',
'Idaho': 'ID', 'Illinois': 'IL', 'Indiana': 'IN', 'Iowa': 'IA',
'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA', 'Maine': 'ME',
'Maryland': 'MD', 'Massachusetts': 'MA', 'Michigan': 'MI',
'Minnesota': 'MN', 'Mississippi': 'MS', 'Missouri': 'MO',
'Montana': 'MT', 'Nebraska': 'NE', 'Nevada': 'NV', 'New Hampshire': 'NH',
'New Jersey': 'NJ', 'New Mexico': 'NM', 'New York': 'NY',
'North Carolina': 'NC', 'North Dakota': 'ND', 'Ohio': 'OH', 'Oklahoma': 'OK',
'Oregon': 'OR', 'Pennsylvania': 'PA', 'Rhode Island': 'RI',
'South Carolina': 'SC', 'South Dakota': 'SD', 'Tennessee': 'TN',
'Texas': 'TX', 'Utah': 'UT', 'Vermont': 'VT', 'Virginia': 'VA',
'Washington': 'WA', 'West Virginia': 'WV', 'Wisconsin': 'WI',
'Wyoming': 'WY', 'American Samoa': 'AS', 'District of Columbia': 'DC',
'Guam': 'GU', 'Northern Mariana Islands': 'MP', 'Puerto Rico': 'PR',
'United States Virgin Islands': 'VI'}
def getParameterInfo(self):
param0 = arcpy.Parameter(
displayName="1- State Street Layer",
name="in_features",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input"
)
param1 = arcpy.Parameter(
displayName="State",
name="state",
datatype="GPString",
parameterType="Required",
direction="Input")
# Set a value list of States (and other US areas)
param1.filter.type = "ValueList"
param1.filter.list = sorted(self.states.keys())
params = [param0, param1]
return params
def execute(self, params, messages):
"""The source code of the tool."""
state = params[1].valueAsText
abbr = self.states[state]
#Do something with state and abbr
return
answered Dec 13, 2017 at 0:49
lang-py