| |||||||||||
| |||||||||||
Defining events including variable values in Tk
With Tcl/Tk, you assign an action to a button when you define the button. The action is often specified by the -command option, and it's important to understand the difference between double quotes and braces in this context. If you put the command in DOUBLE QUOTES, then any $ variable used within the command that's to be run will be interpreted using the variable's value at the time that the button is DEFINED. If you put the command in CURLY BRACES, then any $ variable used within the command that's to be run will be interpreted at the time that the button is PRESSED. For example: #!/usr/bin/wish proc action {ff ss} { puts "$ff and $ss" } for {set k 1} {$k<5} {incr k} { button .first$k -text "First $k" -command "action one $k" button .second$k -text "Second $k" -command {action two $k} pack .first$k .second$k } set k 7 All the buttons with "First" text will give different results when pressed - from 1 to 4. The buttons with "Second" text will all give the result 7 when pressed as that's the value that's been left in the variable k when the tcl/tk process enters its main loop. earth-wind-and-fire:~/feb05 grahamellis$ wish wishdemo one and 1 two and 7 one and 2 two and 7 one and 3 two and 7 one and 4 two and 7 earth-wind-and-fire:~/feb05 grahamellis$ See also Tk Events Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via our main web site
Related Material
Tcl/Tk - Events in Tk resource index - TclTk Solutions centre home page You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum. At Well House Consultants, we provide training courses on subjects such as Ruby, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site. | |||||||||||
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||