3

I am writing a Python toolbox with (among other) 3 parameters: 2 boolean (check boxes) and 1 string. The string parameter's properties depends on the 2 booleans' value:

  • If at least the first boolean is true (box checked), the string (defined initially as disabled and optional) must be set to enabled and required.

  • If only the second boolean is true, the string must remain disabled and optional.

The following code works when opening the tool's interface (string parameter is disabled and optional) and checking the first boolean (string becomes enabled and required). But if I then uncheck it, the string parameter remains enabled and required. How do I make sure my tool checks the state of the boolean parameters at all times?

Working the other way round (string initially enabled and required and changing its state if the booleans are altered) would be fine too.

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

    if params[1].altered:                            # first boolean parameter
        if params[1].value == 1:
            params[8].enabled = 1                    # string parameter
            params[8].parameterType = "Optional"
4

Unfortunately the parameterType property is read only (see here and here), so you cannot change it. However, you can modify the updateMessages function to check for blank string. It might look something like below.

Try this (you will need to modify parameter indices, as mine are 0-2):

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."""
  if not self.params[0].hasBeenValidated: #if boolean 1 changes
      if self.params[0].value == True:
          self.params[2].enabled = True #string parameter
      else:
          self.params[2].enabled = False

  elif not self.params[1].hasBeenValidated: #if boolean 2 changes
      if self.params[1].value == True:
          if self.params[0] == False:
              self.params[2].enabled = False #string parameter
  return

def updateMessages(self):
  """Modify the messages created by internal validation for each tool
  parameter.  This method is called after internal validation."""
  if self.params[0].value == True:
      if self.params[2].value == '':
          self.params[2].setErrorMessage("String required!")
  return

You could always have the string parameter as required, default '', and check it in your code.

  • Thanks a lot, it works. I have actually 3 booleans like the first one so I'll have to work a bit on the logic, but this is a great first step :) – GISGe Jul 8 '15 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.