CheckBox Example

Description

This demonstrates the usage of the CheckBox component and how to read their return values.

Python Script Files

  • check_box_ui.py
  • check_box.py

Significant Script Features

check_box_ui.py:

# Define Layout and Components
def getCustomToolPropertyContent():

   # Create a Grid
   my_Grid = WPFControls.Grid()

   # Add 1 Row and One Column
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())
     
   # Create a vertical array of three check boxes using WrapPanel
   myWrapPanel = WPFControls.WrapPanel()
   myWrapPanel.Orientation=WPFControls.Orientation.Vertical
   
   # Create a List to hold the check boxes
   # Use this later to make it easier to find which button is checked 
   global check_boxes
   check_boxes = [] 

   # Create the first check box
   # Add it to the list and to the WrapPanel
   check_boxes.append(WPFControls.CheckBox())
   check_boxes[0].Content = "Option 1"
   # Check this checkbox
   # Note the horrible notation required to define a Nullable Boolean True
   check_boxes[0].IsChecked = System.Nullable[System.Boolean](True)
   myWrapPanel.Children.Add(check_boxes[0])
 
   # Create a second radio button and add to the WrapPanel
   check_boxes.append(WPFControls.CheckBox())
   check_boxes[1].Content = "Option 2"
   myWrapPanel.Children.Add(check_boxes[1])
   
   # Create a third radio button and add to the WrapPanel
   check_boxes.append(WPFControls.CheckBox())
   check_boxes[2].Content = "Option 3"
   myWrapPanel.Children.Add(check_boxes[2])

   # Position the WrapPanel in the first row, column of the Grid
   WPFControls.Grid.SetRow(myWrapPanel, 0)
   WPFControls.Grid.SetColumn(myWrapPanel, 0)

   # Add the WrapPanel to the Grid 
   my_Grid.Children.Add(myWrapPanel)

   # Return the Grid   
   return my_Grid

# Apply button handler (Green check mark)
# This function is called each time the Apply button is clicked
@apex_sdk.errorhandler
def HandleApplyButton():
   
   # Create a Dictionary to store the user defined tool data  
   dictionary = {}
 
   # Find the checked button in radio_button_array_1
   # And store it in the dictionary 
   # Note that this is somewhat safe since only one radio button can be checked    
   dictionary["check_box_1"] = check_boxes[0].IsChecked
   dictionary["check_box_2"] = check_boxes[1].IsChecked
   dictionary["check_box_3"] = check_boxes[2].IsChecked
   
   # Call the Apex API script using apex_sdk.runScriptFunction()
   # argument 1 = the fully qualified path name of your API script
   # argument 2 = the name of the function in your API script that will receive the Dictionary
   # argument 3 = the Dictionary containing the UI data
   apex_sdk.runScriptFunction(os.path.join(current_file_path, r"check_box.py"), "read_check_boxes", dictionary)

check_box.py:

def read_check_boxes(dict={}):

    import apex

    # Retrieve the active radio buttons from the dictionary
    # Check Box 1
    check_box_1_checked = "not checked"
    if dict["check_box_1"] is "True":
        check_box_1_checked = "checked"

    # Check Box 2
    check_box_2_checked = "not checked"
    if dict["check_box_2"] is "True":
        check_box_2_checked = "checked"

    # Check Box 3
    check_box_3_checked = "not checked"
    if dict["check_box_3"] is "True":
        check_box_3_checked = "checked"

    # Print Check Box Results
    print("Check box 1 is ", check_box_1_checked)
    print("Check box 2 is ", check_box_2_checked)
    print("Check box 3 is ", check_box_3_checked)

Notes

  • Check boxes are used to provide boolean inputs
  • Can be used individually or in arrays
  • Any number of check boxes in an array can be checked or unchecked