For Programmers: Free Programming Magazines  


Home > Archive > Tcl > October 2005 > GUI, namespaces and OO









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author GUI, namespaces and OO
Silas Justiniano

2005-10-29, 7:02 pm

Hello all.

I don't know much about namespaces. I know only to write the following
code:

#start ---
namespace eval my {}

toplevel .t
entry .t.b -textvariable my::myentry
#end ---

If I [destroy .t], the namespace [my] still exists. I could learn some
of the namespace commands to destroy them when the toplevel is
destroyed, or unset all variables automaticaly.

I would like just to know if one of the available OO extensions could
make it automatically for me.

I could pass local variables as parameters to a proc which creates
another toplevel. The problem are binds. Binds don't take variables
from the local scope, I need to use namespaces or globals again.

Thank you very much again.

Silas.

Aric Bills

2005-10-29, 7:02 pm

Snit will do that, and a lot more, for you. Below is a Snit
translation of your code, which will automatically clean up the
variable and namespace when the toplevel is deleted. For something so
trivial, it looks like a lot of extra stuff, but for a real-world
widget of any complexity, the Snit code is likely to be shorter and
guaranteed to be more elegant than the non-Snit version.

The "Snit FAQ" is an excellent tutorial:
http://www.wjduquette.com/snit/snitfaq.html

Regards,
Aric


package require snit

snit::widget twidget {

hulltype toplevel
variable entryvalue
component myentry

constructor {args} {
install myentry using entry $win.myentry \
-textvariable [myvar entryvalue]
grid $myentry
}
}

twidget .t

Eckhard Lehmann

2005-10-30, 7:56 am

Silas Justiniano schrieb:
> Hello all.
>
> I don't know much about namespaces. I know only to write the following
> code:
>
> #start ---
> namespace eval my {}
>
> toplevel .t
> entry .t.b -textvariable my::myentry
> #end ---
>
> If I [destroy .t], the namespace [my] still exists. I could learn some
> of the namespace commands to destroy them when the toplevel is
> destroyed, or unset all variables automaticaly.
>
> I would like just to know if one of the available OO extensions could
> make it automatically for me.


Besides the fact that namespaces come from Itcl, Itk has such a
mechanism. The translation of your code to Itk:

class My {
inherit itk::Toplevel

variable myentry

constructor {args} {
itk_component add b {
entry $itk_interior.b -textvariable [scope myentry]
}
...

eval itk_initialize $args
}
}

# later
My .m
pack .m -expand yes -fill both

# again later
destroy .m


Learn more here: http://wiki.tcl.tk/63


BTW, is it planned to use [scope] with (public) variables from other
Itk/Itcl classes? Like in this short example:

class A {
public variable avar ""
...
}

class B {
inherit itk::Widget

protected variable aobj ""

constructor {args} {
set aobj [A ::#auto]

itk_component add e {
entry $itk_interior.e -textvariable [scope $aobj avar]
}
...
eval itk_initialize $args
}
}

At this moment this does not work, or I've overlooked something. I
managed only to [scope] variables in the current Itk class.


Eckhard
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com