Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TclOO - bind a method to an event

121 views
Skip to first unread message

Markos

unread,
Feb 25, 2018, 9:01:55 AM2/25/18
to
Hi,

Inside a class I'm trying to bind the call of a method to an event with the bind command inside a class.

I created the entry:

set w2 [entry $w1.e -textvar name]

And in the same class the method queryBook:

method queryBook { field } {

puts "method queryBook of [self] field:$field"

}

And associate the method queryBook to <Return> key:

bind $w2 <Return> {
queryBook $name
}

But when I press <Return> the program return the error:

invalid command name "queryBook"
invalid command name "queryBook"
while executing
"queryBook $name"
(command bound to event)

I include "my":

bind $w2 <Return> {
my queryBook $name
}

But again:
invalid command name "my"
invalid command name "my"
while executing
"my queryBook book $::option"
(command bound to event)

And included [self]:

bind $w2 <Return> {
[self] queryBook $name
}

But again the error:

invalid command name "self"
invalid command name "self"
while executing
"self"
invoked from within
"[self] queryBook book $::option"
(command bound to event)

How can I bind the method queryBook to an event in this case?

Thank you,
Markos

Rich

unread,
Feb 25, 2018, 11:11:06 AM2/25/18
to
Markos <c2o.p...@gmail.com> wrote:
> Hi,
>
> Inside a class I'm trying to bind the call of a method to an event
> with the bind command inside a class.
>
> I include "my":
>
> bind $w2 <Return> {
> my queryBook $name
> }
>
> But again:
> invalid command name "my"
> invalid command name "my"
> while executing
> "my queryBook book $::option"
> (command bound to event)
>
> And included [self]:
>
> bind $w2 <Return> {
> [self] queryBook $name
> }
>
> But again the error:
>
> invalid command name "self"
> invalid command name "self"
> while executing
> "self"
> invoked from within
> "[self] queryBook book $::option"
> (command bound to event)
>
> How can I bind the method queryBook to an event in this case?

Neither of the two above are interpreted at the time of definition (the
curly braces see to that).

What happens when you 'do' evaluate one of them at the time of
definition:

bind $w2 <Return> [list [self] queryBook book \$::option]

The backslash above is to prevent the dollar sign from also being
evaluated at definition time, since presumably you want to pass the
contents of ::option as it exists at run time to the method.

Schelte Bron

unread,
Feb 25, 2018, 11:15:43 AM2/25/18
to
Markos wrote:
> Inside a class I'm trying to bind the call of a method to an event
> with the bind command inside a class.
>
See the "Easier Callbacks" section on http://wiki.tcl.tk/21595


Schelte.

Donal K. Fellows

unread,
Feb 26, 2018, 7:52:47 AM2/26/18
to
This is exactly what I'd recommend. Extracting from the page and making
the example a bit more like the original question:

proc ::oo::Helpers::callback {method args} {
list [uplevel 1 {namespace which my}] $method {*}$args
}

oo::class create QueryBook {
method bindTo {widget} {
# Here's how to use it: note that it captures the context from
# where it is called! Also note that callbacks can be unexported
# methods (and it's probably better if they are).

bind $widget <Return> [callback DoQuery %W]

# A real use of it will probably be a bit more complicated...
}

method DoQuery {widgetPath} {
# A very simple callback...
puts "This is [self], being called back about $widgetPath"
}
}

As you can see, that [callback] command is a pretty simple one-liner,
but it does just the right thing in the right place to make calling back
from Tk (or a fileevent or trace or ...) into TclOO object dead easy.
You could also use [mymethod] from Tcllib's oo::util, but I think I like
the name [callback] better. (It internally works in exactly the same way.)

http://core.tcl.tk/tcllib/doc/trunk/embedded/www/tcllib/files/modules/ooutil/ooutil.html

There's a plan to merge the functionality in that module into Tcl.
https://core.tcl.tk/tips/doc/trunk/tip/478.md (The TIP says 8.6.7, but I
think 8.7 is more likely.)

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.

Markos

unread,
Feb 26, 2018, 9:00:05 AM2/26/18
to
Hi,

The command:

bind $w2 <Return> [list [self] queryBook book \$::option]

is working fine. :-)

I will study with more attention the suggestions at:

http://wiki.tcl.tk/21595

And very interesting the suggestion of this procedure:

proc ::oo::Helpers::callback {method args} {
list [uplevel 1 {namespace which my}] $method {*}$args
}


but I confess that I will take some time to better understand the mechanism behind this procedure.

Thank you very much for all help.

Best Regards,
Markos

Schelte Bron

unread,
Feb 26, 2018, 10:54:50 AM2/26/18
to
Markos wrote:
> The command:
>
> bind $w2 <Return> [list [self] queryBook book \$::option]
>
> is working fine. :-)

But only if you call an exported method. This method cannot be used
to invoke an unexported method. The callback command is able to use
both.


Schelte.

0 new messages