Tcl/Tk Cookbook - Basics of Tk


Step 4: Create bindings to modify configuration options with new values

Script

All you now need is to write the procedure reJig, the script of which is given below:


proc reJig { w a} {
	 global aList vList
	 ;# get the index of a in aList
	 set idx [lsearch $aList $a]
	 set va [.rc.fff.sub$idx.ent get]
	 .rc.b config $a $va
	}

The proc command creates the Tcl procedure reJig. The first argument to reJig is a widget name and the second a string (in this case the configure option). The statements within the curly braces is the body of the procedure. ;# indicate that the string enclosed between them and the newline character is a comment.

  1. The global variables used within procedure reJig are declared as the first line of the body.
  2. The index of the current configuration option within aList is accessed by lsearch and assigned to "idx'
  3. The value string in entry widget ".ent" which is a child of .rc.fff.sub$idx is read and assigned to the variable "va"
  4. The widget command .rc.b is called to carry out the action "configure" with the configure option "a" whose value is "va".

The screen dump below shows the result of typing Red in the entry widget opposite -bg.