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.