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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho