Hi. I've just starting tinkering around with XOTcl the past few days, trying to add it to my repertoire of Tcl OO extensions. It's definitely quite an interesting contrast to others like [incr Tcl] or Snit. While I won't be abandoning either of those, XOTcl is currently my leading candidate for a project I'm likely to be working on through the rest of the year, in large part because of its mixin support.
But one issue I couldn't figure out from my reading of the documentation or examples is whether it's possible to use an object's instance variable as a widget's -textvariable or -listvariable. For example, [incr Tcl] supports the [itcl::scope] command, so I can do something like this:
package require Itcl
::itcl::class Toggle { private variable _state "normal"
constructor {} { checkbutton .cb -text Enable \ -variable [::itcl::scope _state] \ -onvalue "normal" -offvalue "disabled" \ -command [::itcl::code $this toggle] .cb select ;# The checkbutton is initially on
# ... }
private method toggle {} { # ... } }
Is there a similar feature for XOTcl?
Thanks,
- Ken Jones
Hi Ken,
perhaps others use other styles, but a simple way to bind variables,commands, or other callbacks from TK into XOTcl is replacement of "self" (or "my") from within a XOTcl method. Example:
Class BrowserTree BrowserTree instproc init args { ... set tree [Tree $sw.tree \ -relief flat -borderwidth 0 -width 15 -highlightthickness 0\ -redraw 0 -dropenabled 1 -dragenabled 1 \ -dragevent 3 \ -droptypes { TREE_NODE {copy {} move {} link {}} LISTBOX_ITEM {copy {} move {} link {}} } \ -opencmd "[self] modifyTree 1 $sw.tree" \ -closecmd "[self] modifyTree 0 $sw.tree"] ... $tree bindText <ButtonPress-1> "[self] selectTreeElement $tree" $tree bindText <Double-ButtonPress-1> "[self] openTreeElement $tree" ... }
note that you cannot use curly brackets {} here, because then self would not be replaced within the object's scope and would likely have a wrong value or raise an exception.
this style of binding works nicely together with XOTcl's inheritance and mixins
Uwe
On Saturday 15 November 2003 00:04, Ken Jones wrote:
Hi. I've just starting tinkering around with XOTcl the past few days, trying to add it to my repertoire of Tcl OO extensions. It's definitely quite an interesting contrast to others like [incr Tcl] or Snit. While I won't be abandoning either of those, XOTcl is currently my leading candidate for a project I'm likely to be working on through the rest of the year, in large part because of its mixin support.
But one issue I couldn't figure out from my reading of the documentation or examples is whether it's possible to use an object's instance variable as a widget's -textvariable or -listvariable. For example, [incr Tcl] supports the [itcl::scope] command, so I can do something like this:
package require Itcl
::itcl::class Toggle {
private variable _state "normal" constructor {} { checkbutton .cb -text Enable \ -variable [::itcl::scope _state] \ -onvalue "normal" -offvalue "disabled" \ -command [::itcl::code $this toggle] .cb select ;# The checkbutton is initially on # ... } private method toggle {} { # ... }
}
Is there a similar feature for XOTcl?
Thanks,
- Ken Jones
Xotcl mailing list - Xotcl@alice.wu-wien.ac.at http://alice.wu-wien.ac.at/mailman/listinfo/xotcl
Uwe Zdun wrote:
Hi Ken,
perhaps others use other styles, but a simple way to bind variables,commands,
or other callbacks from TK into XOTcl is replacement of "self" (or "my") from
within a XOTcl method. Example:
Class BrowserTree BrowserTree instproc init args { ... set tree [Tree $sw.tree \ -relief flat -borderwidth 0 -width 15 -highlightthickness 0\ -redraw 0 -dropenabled 1 -dragenabled 1 \ -dragevent 3 \ -droptypes { TREE_NODE {copy {} move {} link {}} LISTBOX_ITEM {copy {} move {} link {}} } \ -opencmd "[self] modifyTree 1 $sw.tree" \ -closecmd "[self] modifyTree 0 $sw.tree"] ... $tree bindText <ButtonPress-1> "[self] selectTreeElement $tree" $tree bindText <Double-ButtonPress-1> "[self] openTreeElement $tree" ... }
note that you cannot use curly brackets {} here, because then self would not be replaced within the object's scope and would likely have a wrong value or raise an exception.
this style of binding works nicely together with XOTcl's inheritance and mixins
This work perfectly for commands, and I use it all the time, but it does not work for variables. In fact, I believe Tk always expect a Tcl global variable and an XOtcl object instance variable is not a global variable, right?
So the issue is maybe : how to bind an Xotcl object instance variable to a Tcl global variable?
Catherine
Uwe Zdun wrote:
-opencmd "[self] modifyTree 1 $sw.tree" \ -closecmd "[self] modifyTree 0 $sw.tree"] ... $tree bindText <ButtonPress-1> "[self] selectTreeElement $tree" $tree bindText <Double-ButtonPress-1> "[self] openTreeElement $tree"
And of course you always want to write core like the above as:
-opencmd [list [self] modifyTree 1 $sw.tree] or $tree bindText <ButtonPress-1> [list [self] selectTreeElement $tree]
otherwise you are asking for a maintenance headache.
Hi!
The XOTcl are scoped (or can be scoped) in object namespace too. I use it as follow
GUIClass instproc buildEntry win { my requireNamespace entry $win -textvariable [self]::entryValue my set entryValue {Init Value} } requireNamespace is required to build namespace for object. XOTcl object orientation is build (was build) on Tcl namespaces.
You have access to variable per my set userName or instvar userName set userName
I recommend also to use list command to programm callbacks
button $win -text "My Button" -command [list [self] buttonPressed]
Look at XOTclIDE http://www.xdobry.de/xotclIDE component (IDEBaseGUI) to have many examples how to bind XOTcl with Tk
Artur Trzewik
At 11:12 AM 11/15/2003 +0100, Artur Trzewik wrote:
The XOTcl are scoped (or can be scoped) in object namespace too. I use it as follow
GUIClass instproc buildEntry win { my requireNamespace entry $win -textvariable [self]::entryValue my set entryValue {Init Value} } requireNamespace is required to build namespace for object. XOTcl object orientation is build (was build) on Tcl namespaces.
Ahh.... That's exactly what I was looking for. The documentation mentions that XOTcl is built on Tcl namespaces, but is a little vague in exactly how, aside from the discussion of aggregation. And I'd managed to overlook the documentation for the "requireNamespace" method. Actually, even after seeing your example, I don't think that the current "requireNamespace" method documentation is quite clear as to why it's needed. May I suggest adding a sentence or two to its description basically outlining an application of it, such as this, to help other newcomers to XOTcl understand its use?
Thanks for the clarification!
- Ken
Hi all,
I'm currently updating the XOTcl documentation for the next release, 1.1, and due to this discussion I've added a section "Integrating XOTcl Programs with C Extensions (such as TK)" to the tutorial ... perhaps the contributors to this discussion might want to review this doc. Perhaps you use some practices I've forgotten?
(It's the last section in the attached file)
Uwe