[GIS] python script tool validation checkboxes to radio boxes

arcgis-desktoparcpybooleanpython-script-tooltool-validation

I am trying to build a tool in arcmap, which uses radio buttons in its gui. Since I read, that radio buttons are not supported, I figured, it couldn't be too hard changing the behaviour of simple boolean checkboxes, so that they act like actual radio buttons (no mutliple selections allowed).

So I went on and created three test parameters with datatype boolean. Next I added the following code to the tool validation function "def updateParameters" :

def updateParameters(self):
    if self.params[0].value == True:
        self.params[1].value = False
        self.params[2].value = False

    if self.params[1].value == True:
        self.params[0].value = False
        self.params[2].value = False

    if self.params[2].value == True:
        self.params[0].value = False
        self.params[1].value = False
return

but it produces some odd behaviour:
When I start out by selecting the third box (resp. set params[2] to true) and continue with clicking on the first checkbox, then the tool acts like it's supposed to (by unchecking params[2] and checking params[0]). But that's pretty much it. If I click the checkboxes in any different chronological order sometimes, two checkboxes can be set to true, sometimes no other than the active one can be clicked and so on.

Can anybody reproduce this error? Or maybe tell me what I do wrong?

I'm guessing when I check the first checkbox in the beginning, the other checkboxes are set to false until I uncheck it (which I could at least comprehend). But following that logic, why can I check the first box right after I checked the third one. Shouldn't the first one be also set to false and therefore be uncheckable?

Does anybody have an idea how to approach this?


okay, since Richard answered frist, I played around with his approach first. Can't get it to work though. I set up the global parameter right at the start of the code (before class ToolValidator) and tried to get it in the function updateParameters by adding "global lastChecked" inside the updateParameters.

import arcpy

lastChecked = 0 # here I set up the global parameter

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."""

    global lastChecked #here I try to get it in the function

    #number 1
    if self.params[0].value == False and self.params[1].value == False and self.params[2].value == False: 
        self.params[lastChecked].value = True
    #number 2
    if lastChecked != 0 and self.params[0].value == True:
        self.params[1].value = False
        self.params[2].value = False
        lastChecked = 0
    #number 3
    if lastChecked != 1 and self.params[1].value == True:
        self.params[0].value = False
        self.params[2].value = False
        lastChecked = 1
    #number 4
    if lastChecked != 2 and self.params[2].value == True:
        self.params[0].value = False
        self.params[1].value = False
        lastChecked = 2
    return


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

First he goes inside the first control structure (number 1 in the code) and sets the checkbox one to True, which is right. Then, I click the second checkbox and he goes into control structure 3 (number 3), also okay. But now, if I try to click either checkbox 1 or 3 he will still enter control structure 3 because lastChecked is back to zero again and two checkboxes are checked (the second and one of the others). I can't seem to save the global parameter. If I could manage that, the code would work just fine. I tested it in an extern python script with slightly modified parameter names.

so:


state 1) entering the script

  • lastChecked: 0
  • cb0 before: False
  • cb1 before: False
  • cb2 before: False

leads to:

  • lastChecked: 0
  • cb0 after: True
  • cb1 after: False
  • cb2 after: False

state 2) the state, when CB 1 was checked and I click CB 2

  • lastChecked: 0
  • cb0 before: True
  • cb1 before: True
  • cb2 before: False

leads to:

  • lastChecked: 1

  • cb0 after: False

  • cb1 after: True
  • cb2 after: False

state 3) the state when CB2 is checked and I click CB 3

  • lastChecked: 0
  • cb0 before: False
  • cb1 before: True
  • cb2 before: True

leads to:

  • lastChecked: 1
  • cb0 after: False
  • cb1 after: True
  • cb2 after: False

which is the same result as from state 2 because lastChecked is 0 again instead of 1 when it enters

I hope I could illustrate the problem. How can I save into the global variable? I thought this was possible by using the global statement? Or did you mean, that I should define the lastChecked parameter somewhere else? If so, how could I get it from f.e. the initializeParameters function into the updateParameters function?

Best Answer

Try setting it up this way. Define a lastChecked global variable to hold the value of 0 to represent the first checkbox and by default make sure the first check box is checked at tool initialization.

First check if the user set all checkboxes to False, and if they did, reset the lastChecked checkbox back to True (radio buttons cannot be turned off by clicking on them, but checkboxes can be). Then make each of your three original if conditional statements first determine that the lastChecked variable does not contain the number representing the checkbox being validated by that if statement. This way it will only apply settings based on the True checkbox that was not previously checked and uncheck all others. A maximum of two boxes can be simultaneously set to true by the user when the method fires and one of them will be stored in the lastChecked variable and the other won't.

# Set up a global parameter at initialization
lastChecked = 0
# Set the checkbox 0 parameter to True here or set it up in the toolbox definition.

def updateParameters(self):
    if self.params[0].value == False and self.params[1].value == False and self.params[2].value == False: 
        self.params[lastChecked].value = True

    if lastChecked != 0 and self.params[0].value == True:
        self.params[1].value = False
        self.params[2].value = False
        lastChecked = 0

    if lastChecked != 1 and self.params[1].value == True:
        self.params[0].value = False
        self.params[2].value = False
        lastChecked = 1

    if lastChecked != 2 and self.params[2].value == True:
        self.params[0].value = False
        self.params[1].value = False
        lastChecked = 2
return
Related Question