Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
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
  [4258] Keeping you Tkinter display up to date while monitoring - (2014-04-06)
  [3575] Multiple buttons calling the same proc in wish (tcl/tk) - (2012-01-12)
  [1473] Making a variable dynamically visible in a Tcl/Tk GUI - (2007-12-15)
  [386] What is a callback? - (2005-07-22)

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, Lua, 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.

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/solutions/tcl-defi ... in-tk.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard