Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Tk - specifying commands on a widget

Posted by admin (Graham Ellis), 28 July 2002
One of the other issues that often catches newcomers is the difference between each of the following:

     -command {others have}    ; # 1
     -command {others $have}    ; # 2
     -command "others $have"    ; # 3
     -command [others $have]    ; # 4

Potentially, all four of these are valid in a widget or megawidget constructor.

The fourth example instructs Tcl to run the others command prior to building the widget, passing it a single parameter which is the contents of the $have variable prior to the construction of the widget. The value returned by the others command is the name of the proc which is to be run whenever the value in the widget is altered by the user or some other piece of program. Note that this example, whilst quite valid, is the least likely of our four examples to be used.

The third example uses double quotes around the command. The value of $have is substituted in at the time the widget is constructed. When the widget's -command is run, it will run "other" with two parameters, the first being the value that was in the $have variable at the time that the widget was built. The second being the value selected on the widget. Note that this second parameter only applies to a tixControl widget, a scale widget and certain others that append the return value to the command automatically. This example is likely to be how you want to write your code if you're using a foreach loop to build your widgets and want each callback to include a unique parameter indicating which widget was selected.

The second example, using curly braces around the command, requests deferred execution. In other words, the "other" command will be run at the time the callback is activated, and it's at that time that the value of $have will be substituted. This is a major difference to the two cases that we have just looked at, where the variable $have's value at setup time is used, rather than its dynamic value as the program has been running.

The first (and final) example doesn't refer to a variable called "have" directly; it passes the text "have" across to the callback proc. Notice that there is no $ character. You'll write callback code like this if you want to pass a variable name into the callback routine. If you look at the source code of the callback routine you'll probably find that it starts straight away with an upvar command. Although the code may appear obtuse, this is a very common structure and indeed it's the one that we used in the example that lead to this discussion.

Summarising. As you write callback procs, start by asking yourself "Do I want to pass in a variable name, its value now, its value later, or the result of running a command on it?"



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho