AnsweredAssumed Answered

Getting value from Python Toolbox ValueList Parameter

Question asked by johnmdye on Oct 20, 2017
Latest reply on Oct 25, 2017 by johnmdye

I was slapping together a toolbox tool today and came across an interesting issue.

import arcpy

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""

        self.label = "Toolbox"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [Tool]

class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

    def MessageBox(self, Title, Message, ButtonStyle=0, MessageboxType="NOICON"):
        """
        Raises a system-modal messagebox with the given title and message
        with the specified ButtonStyle and MessageboxType. Used to raise
        messageboxes from a Python Toolbox tool operating on a Background Thread.

        INPUTS:
            Title (String)
                Title of the messagebox
            Message (String)
                Message to be displayed in the messagebox
            ButtonStyle (Integer)
                Value:                   Buttons Shown:
                0                        OK (Default)
                1                        OK | Cancel
                2                        Abort | Retry | Ignore
                3                        Yes | No | Cancel
                4                        Yes | No
                5                        Retry | No
                6                        Cancel | Try Again | Continue
            MessageboxType (String)
                Value:                   Description:
                NOICON                   No icon is displayed in the Messagebox
                INFO                     An Information Icon is displayed
                QUESTION                 A Question Mark Icon is displayed
                WARNING                  A Warning Icon is displayed
                ERROR                    An Error Icon is displayed

        OUTPUTS:
            Button Clicked (Integer)
                Value returned indicates the buttonpressed by the user

                Return Value             Button Pressed
                1                        OK
                2                        Cancel
                3                        Abort
                4                        Retry
                5                        Ignore
                6                        Yes
                7                        No
                10                       Try Again
                11                       Continue
        """

        if MessageboxType == "NOICON":
            MB_STYLE = 0x00
        elif MessageboxType == "INFO":
            MB_STYLE = 0x40
        elif MessageboxType == "QUESTION":
            MB_STYLE = 0x20
        elif MessageboxType == "WARNING":
            MB_STYLE = 0x30
        elif MessageboxType == "ERROR":
            MB_STYLE = 0x10
        else:
            raise Exception("Invalid 'MessageboxType' value")

        if ButtonStyle not in range(0, 7):
            raise Exception("Invalid 'ButtonStyle' value")

        import ctypes
        buttonPressed = ctypes.windll.user32.MessageBoxA(0
                                            , Message
                                            , Title
                                            , ButtonStyle | MB_STYLE | 0x40000)
        return buttonPressed

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="GPString",
            parameterType="Required",
            direction="Input")
        param0.filter.type="ValueList"
        param0.filter.list=["Butter", "Milk", "Bread"]

        params = [param0]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

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

        if parameters[0].value:
            self.MessageBox("Selected Parameter", parameters[0].valueAsText)
        return

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

        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        arcpy.AddMessage("Parameter 0: " + parameters[0].valueAsText)
        return

There's nothing crazy going on here. Only thing out of the ordinary is that MessageBox function.

 

Here's the weird thing. My updateParameters function has some logic to raise a Messagebox that displays the value selected by the user in param0. Works fine except that for some reason, the value is being sliced - I'm only getting the first letter of the selected parameter.

I also added an AddMessage function to the execute function to print the selected parameter to the Results Window. That actually gives me the full value, so perhaps it is my Messagebox function that is slicing it but I don't think so.

 

Anyone have any insight?

Outcomes