Purpose: explain the Tk concept of bindings. List the different kinds of bindings available. Show a practical example of use. ---- Bindings are the connections between widgets that create events, and the code (procedures) that handle the events. proc makeButton { buttonName buttonText } { button .$buttonName -text $buttonText bind .$buttonName "$buttonText" bind .$buttonName "destroy .$buttonName" bind .$buttonName "puts \"$buttonText\"" pack .$buttonName } ---- Bindings are a specialisation of the general concept of event handlers to Tk's external event mechanism, and they are needed because GUIs are asynchronous things; you don't know what a user is going to do next, and you have to handle a lot of different possibilities. The only alternative to the system of having events is to use a finite state machine, but writing one of those for a modern GUI is a terrifying concept! Most system programming languages use callbacks to handle events, but in Tcl it is far more natural to directly nominate a piece of code to execute whenever a particular event arrives, especially as this can always call a procedure if needed. It is this code that is called a binding (because of the command used to manage this association; '''bind'''.) But now for an example: pack [label .l -textvariable msg] set msg "Dum de dum de dum..." bind .l {set msg "Hello friend"} bind .l {set msg "You're touching me!"} bind .l {set msg "You've let go."} bind .l {set msg "Dum de dum de dum..."} bind .l <3> {bell} ---- [Ken Jones] explains how to think about default bindings in a [Usenet] posting [http://groups.google.com/groups?q=tkbuttonenter+args+group%3acomp.lang.tcl&hl=en&lr=&scoring=d&selm=d8f00cc1.0205140534.2c0dfd53%40posting.google.com&rnum=1]. ---- Bryan Oakley has a very readable explanation of bindings in http://www.tclscripting.com/articles/mar06/article2.html ----