在Python脚本工具的参数中设置下拉列表?


10

我正在尝试从我编写的python脚本创建一个工具,该工具将使用我创建的列表,并将其用作完成的工具中的下拉菜单,作为输入之一(例如,请参见随附的图片):

在此处输入图片说明

我正在使用的列表是一个很大的列表,其中包括佛蒙特州的所有城镇,我从表格中的脚本中生成它(请参见下面的代码)。我怀疑目前我的问题只是设置工具“属性”以获取此列表并使用它为用户创建一个下拉列表。这是创建用于参数的列表的代码块-有人看到该工具的此代码结束有任何问题吗?

import arcpy
arcpy.env.workspace = "Z:\\OPS\\TechnicalServices\\Culverts\\GetCulverts\\GetCulverts.gdb"
towns = "Database Connections\\GDB_GEN.sde\\GDB_Gen.VTRANS_ADMIN.townindex"
arcpy.MakeFeatureLayer_management(towns,"towns_lyr")

NameList = []
NameListArray = set()
rows = arcpy.SearchCursor("towns_lyr")
for row in rows:
    value = row.getValue("TOWNNAME")
if value not in NameListArray:
    NameList.append(value)
town = NameList

town = arcpy.GetParameterAsText(0)

这也是工具属性的图像,带有默认的验证代码-我需要更改此验证代码吗?

我寻找了有关更改此验证码的信息,但找不到有关将其用于格式化下拉列表的信息。

在此处输入图片说明

Answers:


7

尝试将工具验证器类代码设置为此:

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."""
    towns = "Database Connections\\GDB_GEN.sde\\GDB_Gen.VTRANS_ADMIN.townindex"
    rows = arcpy.SearchCursor(towns)
    self.params[0].filter.list = sorted(list(set(r.getValue('TOWNNAME') for r in rows)))
    del rows
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.