RadioButton Example

Description

This demonstrates the RadioButton UI component with horizontal and vertical arrays returning their current button selection.

Python Script Files

  • radio_button_ui.py
  • radio_button.py

Significant Script Features

radio_button_ui.py:

# Define Layout and Components
def getCustomToolPropertyContent():

   # Create a Grid
   my_Grid = WPFControls.Grid()

   # Add 6 Rows and One Column
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition()) 
   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())
   
   # Create a label for the first radio button array
   meshTextBlock = WPFControls.TextBlock()
   meshTextBlock.Text = "Vertical Array"
   WPFControls.Grid.SetRow(meshTextBlock, 0)
   WPFControls.Grid.SetColumn(meshTextBlock, 0)

   # Create a vertical array of three radio buttons
   # Create a WrapPanel   
   vWrapPanel = WPFControls.WrapPanel()
   vWrapPanel.Orientation=WPFControls.Orientation.Vertical
   
   # Create a List to hold the radio buttons in the first array
   # We'll use this later to make it easier to find which button is checked 
   global vertical_radio_buttons
   vertical_radio_buttons = [] 

   # Create a first radio button
   # Add it to the list and to the WrapPanel
   vertical_radio_buttons.append(WPFControls.RadioButton())
   vertical_radio_buttons[0].Content = "Option 1"
   # Set the first radio button to be checked
   # Note the horrible notation required to define a Nullable Boolean True
   vertical_radio_buttons[0].IsChecked = System.Nullable[System.Boolean](True)
   vWrapPanel.Children.Add(vertical_radio_buttons[0])
 
   # Create a second radio button and add to the WrapPanel
   vertical_radio_buttons.append(WPFControls.RadioButton())
   vertical_radio_buttons[1].Content = "Option 2"
   vWrapPanel.Children.Add(vertical_radio_buttons[1])
   
   # Create a third radio button and add to the WrapPanel
   vertical_radio_buttons.append(WPFControls.RadioButton())
   vertical_radio_buttons[2].Content = "Option 3"
   vWrapPanel.Children.Add(vertical_radio_buttons[2])

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

   # Add the WrapPanel to the Grid
   my_Grid.Children.Add(meshTextBlock)   
   my_Grid.Children.Add(vWrapPanel)

   # Second Button Array
   # Create a label for the second radio button array
   meshTextBlock2 = WPFControls.TextBlock()
   meshTextBlock2.Text = "Horizontal Array"
   WPFControls.Grid.SetRow(meshTextBlock2, 2)
   WPFControls.Grid.SetColumn(meshTextBlock2, 0)

   # Create a horizontal array of three radio buttons
   # Create a WrapPanel   
   hWrapPanel = WPFControls.WrapPanel()
   hWrapPanel.Orientation=WPFControls.Orientation.Horizontal

   # Create a List to hold the radio buttons in the first array
   # We'll use this later to make it easier to find which button is checked 
   global horizontal_radio_buttons
   horizontal_radio_buttons = [] 
   
   # Create a first radio button and add to the WrapPanel 
   horizontal_radio_buttons.append(WPFControls.RadioButton())
   horizontal_radio_buttons[0].Content = "Option 1"

   # Set the first radio button to be checked
   # Note the horrible notation required to define a Nullable Boolean True
   horizontal_radio_buttons[0].IsChecked = System.Nullable[System.Boolean](True)
   hWrapPanel.Children.Add(horizontal_radio_buttons[0])
 
   # Create a second radio button and add to the WrapPanel
   horizontal_radio_buttons.append(WPFControls.RadioButton())
   horizontal_radio_buttons[1].Content = "Option 2"
   hWrapPanel.Children.Add(horizontal_radio_buttons[1])
   
   # Create a third radio button and add to the WrapPanel
   horizontal_radio_buttons.append(WPFControls.RadioButton())
   horizontal_radio_buttons[2].Content = "Option 3"
   hWrapPanel.Children.Add(horizontal_radio_buttons[2])

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

   #Add the WrapPanel to the Grid
   my_Grid.Children.Add(meshTextBlock2)
   my_Grid.Children.Add(hWrapPanel)

   # 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_vertical
   # And store it in the dictionary 
   # Note that this is somewhat safe since only one radio button can be checked
   vertical_checked_content = [x for x in vertical_radio_buttons if x.IsChecked == True][0].Content          
   dictionary["vertical_checked"] = vertical_checked_content

   # Find the checked button in radio_button_horizontal
   # And store it in the dictionary 
   horizontal_checked_content = [x for x in horizontal_radio_buttons if x.IsChecked == True][0].Content          
   dictionary["horizontal_checked"] = horizontal_checked_content
   
   # 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"radio_button.py"),
                              "radio_button",
                              dictionary)

Notes

  • Radio button arrays are used to choose a single option from a group of options
  • One radio button in the array is always checked
  • Selecting any unchecked radio button checks it and unchecks the previously selected button
  • Radio button arrays can be laid out vertically or horizontally
  • The checked button from any array can be established and passed to your Apex API script