Select to view content in your preferred language

Select a String Parameter Value

2297
3
01-16-2014 05:39 PM
CPoynter
Frequent Contributor
Hi,

I am using GetParameterAsText. I want to be able to select a value (i.e. A or B) for my response from a prompted list.

How can I code a prompt list to appear with set values? Is this possible within ArcPy without having to use an add-in?

Regards,

C. Poynter
Tags (2)
0 Kudos
3 Replies
by Anonymous User
Not applicable
You have to do this in the tool" rel="nofollow" target="_blank">http://resources.arcgis.com/en/help/main/10.1/index.html#//0015... validator class for your script tool.

Here is an example:

import arcpy
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""

    # List for parameter
    self.params[0].filter.list = ['Option A', 'Option B', 'etc']
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return


This will create a drop down list for the first parameter of a script tool.
0 Kudos
CPoynter
Frequent Contributor
Hi Caleb,

That looks very promising. Will be trying it. Is the list auto-populated into a tool when script is imported into ArcToolbox or do properties need setting? The on-line help doesn't make things real clear.

Regards,

Craig
0 Kudos
by Anonymous User
Not applicable
Yes, the drop down list will be automatically populated in the tool interface after you set up the tool's validation. This can be set up in the 'Validation' tab from the script tool properties.

[ATTACH=CONFIG]30703[/ATTACH]

From inside the Validation tab, you need to choose to edit the script in the lower right corner. Esri provides every script tool with this tool validator class template. For making drop down lists, you just need to alter the 'updateParameters' method. In this case, my validation code looks like this (drop down lists highlighted in red):

import os, fnmatch

# function to find files with wildcard
def find(pattern, path):
    theFiles = []
    for path, dirs, files in os.walk(path):
        for filename in files:
            if fnmatch.fnmatch(filename, pattern):
                theFiles.append(os.path.abspath(os.path.join(path, filename)))
    return sorted(theFiles)

class ToolValidator:
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    import arcpy
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""

    # Path to SQL Databases
    sde_loc = r'\\arcserver1\SDE'
    sde_db = find('*.mdf', sde_loc)

    # drop down lists
    self.params[0].filter.list = [os.path.basename(s).split('.')[0] for s in sde_db]
    self.params[2].filter.list = ['DBO.DEFAULT', 'DBO.EDITOR']
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return


Once you have added your validation, when you open the tool you should have little black arrows in the lower right of each parameter for which you set up a list. And when you click on the arrow, your list should appear.

[ATTACH=CONFIG]30704[/ATTACH][ATTACH=CONFIG]30705[/ATTACH]
0 Kudos