Re: adding procedures to buttons
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: adding procedures to buttons

From: Schelte Bron <nospam@wanadoo.nl>
Date: Fri Jul 01 2005 - 22:32:19 CEST

Bryan Oakley wrote:
> Assuming buttons are created with the 'button' command, you can do this:
>
> rename button _button
> proc button {args} {
> set button [uplevel _button $args]
> set cmd [$button cget -command]
> $button configure -command "$cmd; otherCode"
> return $button
> }
>
Nice idea! Continuing from that you could also do:

   rename button _button
   proc button {args} {
     set button [uplevel 1 _button $args]
     set cmd [$button cget -command]
     $button configure \
        -command [list event generate $button <<ButtonInvoke>>]
     bind $button <<ButtonInvoke>> $cmd
     return $button
   }

This way you can more easily handle the situations where the command needs
to be changed at a later time. Instead of:
   .foo configure -command newCmd
you just have to do:
   bind .foo <<ButtonInvoke>> newCmd

And whenever you want to add a command to a single button, you do:
   bind $button <<ButtonInvoke>> +otherCode

Or to add a command to all buttons:
   bind Button <<ButtonInvoke>> otherCode

Since the default bindtags cause the Button bindings to happen after the
individual button's bindings, that should give you the result you are
looking for.

Another advantage of this method is that you can selectively skip the common
code by returning a break condition from the individual button command:
   return -code break

Schelte.

-- 
set Reply-To [string map {nospam schelte} $header(From)]
Received on Thu Sep 29 14:24:30 2005