Next: Accessing The Components Up: Getting Started: the Previous: Disabling Callbacks Temporarily


Validating User Inputs 

Sometimes it may be necessary to check the user input against certain criteria. For example, you may want to allow only even numbers in a TixControl widget. To do this, you can use the -validatecmd option, which specifies a Tcl command to call whenever the user enters a new value. Here is an example:

tixControl .c -value 0 -step 2 -validatecmd evenOnly

proc evenOnly {value} {
return [expr $value - ($value %2)]

The value parameter to evenOnly is the new value entered by the user. The evenOnly procedure makes sure that the new value is even by returning a modified, even number. The Tcl command specified by the -validatecmd must return a value which it deems valid and this value will be stored in the -value option of the TixControl widget.