Attaching commands to UI elements

 
 
 

After creating a window containing all the elements that you require you will want it to do something. Each control can execute MEL commands or procedures triggered by user actions. The types of actions that are supported for each control depend upon the nature of that control. For example buttons only support the execution of a command when they are pressed, whereas sliders support commands when they are dragged and also when they change value. See the command documentation for the list of callbacks supported by each control.

A simple example is to attach a command to a button. The following command will change the button’s label text when the button is pressed.

Script 5. Simple Functionality

window -width 200 -title "Test Window" ExampleWindow5;
 columnLayout;
 // Create the button.
 // 
 string $button = `button -label "Initial Label"`;
 // Add the command.
 // 
 string $buttonCmd;
 $buttonCmd = ("button -edit -label \"Final Label\" " + $button);
 button -edit -command $buttonCmd $button;
showWindow ExampleWindow5;

In this example a single command is attached to the button. It is equally easy to attach procedures with arguments. The following example slightly modifies the above example.

Script 6. Simple Functionality in a Procedure

window -title "Test Window" -widthHeight 200 100 ExampleWindow6;
 columnLayout;
 // Create the button.
 // 
 string $button = `button -label "Initial Label"`;
 // Add the command.
 // 
 button -edit -command ("changeButtonLabel " + $button) $button;
showWindow ExampleWindow6;
proc changeButtonLabel (string $whichButton) {
 string $labelA;
 string $labelB;
 string $currentLabel;
 
 $currentLabel = `button -query -label $whichButton`;
 $labelA = "New Label A";
 $labelB = "New Label B";
 if ($currentLabel != $labelA) {
 button -edit -label $labelA $whichButton;
 } else {
 button -edit -label $labelB $whichButton;
 }
}

Often the value of the control is needed as a parameter in the command that it issues. To avoid querying the control each time its state changes, its value can be symbolically embedded in the command as the string “#1”. When the control changes value then the “#1” will be substituted with the actual value of the control at the time the command is issued. Groups with multiple values use “#2”, “#3”, etc. for the values of their different components. For example, a float field group with three fields can represent the values of each of those fields in its commands with “#1”, “#2”, “#3” respectively.

Often you will want to have a control show the value of a node’s attribute and update when that attribute changes. The easiest way to do this is to use the ’attr’ versions of the controls; that is, attrFieldGrp instead of floatFieldGrp. If an ’attr’ command doesn’t exist then use the connectControl command.

Creative Commons License Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License