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 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Call back procedures and the TK Button Command

Posted by Graham_Tasker (Graham_Tasker), 15 February 2005
The information for theTk command button -command state that it will invoke any Tcl Command and there fore it should be possible to invoke a procedure defined using the Tcl proc name {params} {body} structure. Am I correct?

Therefore would the following be possible :

proc setUpAbort { callbackProc } {

   button .abortButton -text "ABORT" \
               -command $callbackProc     \
   pack .abortButton
}


and is there anything special about the structure of the callback procedure?

With Thanks : Graham Tasker.

P.s. This may seam a trivial question but Graham was not allowed to cover much Tk on the corse he gave us.

Posted by admin (Graham Ellis), 16 February 2005
Yes, you can put a proc call there.

And ... the question is NOT trivial.   If you use any $variables in the -command section you have to be VERY careful whether they're in double quotes or not protected (in which cased the value of the variable at the time the widget is DEFINED is used) or in curly braces (in which case it's the value of the variable when the widget is PRESSED)

Catches trainees every time  

Posted by Graham_Tasker (Graham_Tasker), 16 February 2005
Ho hell, I see that I have opened a can of worms

Right  then back to my example above, I take it that the the command call is defined when the button widget it defined.

But what happens if the procedure that is called contains $variables, when are thay defined? when the button widget is defined when the procedure gets called? This will make a considerable difference to how the abort process is written.

Posted by admin (Graham Ellis), 16 February 2005
I think the simple answer is "when the command is run" since they'll be in a { } block as you define the proc, won't they. (PLEASE say that they will and that you're not writing your proc totally with " and \ protection     .. sorry - silly day)

As a subtext to my earlier answer, I have a note to write this up much more fully and post it into the solution centre.

Posted by admin (Graham Ellis), 19 February 2005
on 02/16/05 at 19:54:11, Graham Ellis wrote:
As a subtext to my earlier answer, I have a note to write this up much more fully and post it into the solution centre.


OK - I have written it up as an article and with an example. See
http://www.wellho.net/solutions/tcl-defining-events-including-variable-values-in-tk.html

Posted by Graham_Tasker (Graham_Tasker), 21 February 2005
Thanks for the solutions, but in a way it has made things worse, you state :

If you put the command in DOUBLE QUOTES, then any $ variable used within the command that's to be run will be interpretted 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 interpretted at the time that the button is PRESSED.


but if you look at tthe example as follows :

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


and its output

one and 1
two and 7
one and 2
two and 7
one and 3
two and 7
one and 4
two and 7


One and 1 relate to the output generated by the Double quotes call, and two and 7 to the Curly Braces call, Or have a get my self in knots?? please confirm.




Posted by admin (Graham Ellis), 21 February 2005
The variable k steps up 1, 2, 3, 4 as the buttons labelled with the word FIRST are defined ... so that the buttone when pressed later on reflect the values 1 2 3 and 4 which was what WAS in the k variable when they were set up.

The buttons labelled with the word SECOND have a reference to $k made in the deferred block of code, so that the k variable is only looked up when the button is pressed, by which time we've set it to 7.

The best way to get a feel for this is to cut and paste an example and play with it.  It seems a very strange business at first!



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., 2022: 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