Select to view content in your preferred language

Disable "add another" in Python Toolbox

718
3
01-12-2024 10:20 AM
kim_hspartner
Emerging Contributor

I'm creating a python toolbox and can't figure out how I can prevent the "Add Another"  button from appearing.  Below is the interface of my tool and the code that shows the property of the second parameter, Arterial Weights.  I figured setting the `multiValue=False` would do the trick, but it didn't work.  🤔

kim_hspartner_0-1705083341344.png

    param = arcpy.Parameter(
        displayName='{0} Weights'.format(mode_name.capitalize()),
        name='{0}_weights'.format(mode_name),
        datatype='GPValueTable',
        parameterType='Required',
        direction='Input',
        multiValue=False
    )

 I 

Tags (1)
3 Replies
DavidWynne
Esri Contributor

Hi Kim,

Yes, there's a way to do this. It's not that obvious.

For some parameters, there are extra control types that will support a different appearance and behaviors.

What you're seeing is the default control for a parameter with a Value Table data type. By default, someone can add multiple sets of values.

But, you can modify this default behavior by setting the parameter object's controlCLSID property.

So, for example, with my own code, if I create a Value Table parameter with the default control, you get this (similar to your example):

        param1 = arcpy.Parameter(
            displayName='Arterials Weights',
            name='arterials_weights',
            datatype='GPValueTable',
            parameterType='Required',
            direction='Input',
        )

        param1.columns = [['GPString', 'A'], ['GPString', 'B'], ['GPString', 'C'], ['GPString', 'D'], ['GPString', 'E']]

 

DavidWynne_0-1706752911499.png

 

If I use the same parameter information, but then also set the controlCLSID value to a string of '{1A1CA7EC-A47A-4187-A15C-6EDBA4FE0CF7}', I get the following appearance, limiting the Value Table to single set of values.

        param1 = arcpy.Parameter(
            displayName='Arterials Weights',
            name='arterials_weights',
            datatype='GPValueTable',
            parameterType='Required',
            direction='Input',
        )

        param1.columns = [['GPString', 'A'], ['GPString', 'B'], ['GPString', 'C'], ['GPString', 'D'], ['GPString', 'E']]
        param1.controlCLSID = '{1A1CA7EC-A47A-4187-A15C-6EDBA4FE0CF7}'

 

DavidWynne_1-1706753030891.png

 

There's some other examples here: https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/parameter-controls.htm. It's not a complete list, and it doesn't include this one (it should).

Does that help? Let me know if you have any questions.

-Dave

DuncanHornby
MVP Notable Contributor

David,

This is a really interesting answer, a property of parameters I was unaware of. I'm developing a toolbox (the python code is embedding in the atbx) and was wondering if what you suggest can work for a specific scenario.

ESRI have have introduced that switch under parameters that take layers\tables  with a selection. If the layer has a selection then the switch appears in an on state.

Can your technique you show above  be used to hide the switch? It causes conflicts with the design of my tools and the code to fail if a user chooses to switch it off.

Duncan

0 Kudos
DavidWynne
Esri Contributor

Hi Duncan, 
Unfortunately, no, there isn't a different parameter control that will disable the selection toggle. If the tool expects and requires a selection, you could do something like what the Eliminate tool does, which checks if the input has a selection upfront in the execution code.

In Python, you could use Describe's FIDSet property such as below:

 

in_features = arcpy.GetParameterAsText(0)

desc = arcpy.Describe(in_features)
if not getattr(desc, 'FIDSet', ''):
    arcpy.AddError('Input must have a selection.')
    sys.exit()

 

 

Note: I've used `getattr` to add some flexibility, since if the input is a feature class (not a layer), `FIDSet` isn't available from Describe.