From: Silas on
Hello all,

Some years ago I used Tcl 8.4 to create a relatively complex
application. I didn't use many of the advanced techniques Tcl EIAS
allowed, so I built it almost entirely pure imperative. Switched job
and programming languages. After 3 years using C++ and Qt I switched
back to Tcl to develop a small graphical tool.

I realized that my mind was already settled on C++/Qt OO way so I
tried to get OO on Tcl too. I have already studyied a bit of XOTcl,
but since 8.6b1 is out now, I decided to give it a chance. TclOO works
great and I can use most of the XOTcl features I used too (not the
most advanced ones, but that is ok).

The problem comes when I try to mix a well designed OO system with Tk.
Tk widgets are always global and callbacks (the ones you bind with -
command) also need to be global. I found some interesting references,
but the doubt still persists. The references are:

http://wiki.tcl.tk/21103
http://groups.google.com/group/comp.lang.tcl/browse_thread/thread/4defab229c59e325/f4eb65beb949ed1e?lnk=gst&q=oo+gui#f4eb65beb949ed1e

My question is more general. Even before OO, I've never been satisfied
that, after a well-built application, with correct function calling
and encapsulation by scope, I had to deal with widgets declared
globally. Since we can't just get rid or that feature, I'd like to
ask: what are the practices you adopt when building your Tk
applications? How would you layer your application in pure-Tcl core
and Tk frontend?

Thank you!
From: Arjen Markus on
On 5 mrt, 20:32, Silas <sila...(a)gmail.com> wrote:
> Hello all,
>
> Some years ago I used Tcl 8.4 to create a relatively complex
> application. I didn't use many of the advanced techniques Tcl EIAS
> allowed, so I built it almost entirely pure imperative. Switched job
> and programming languages. After 3 years using C++ and Qt I switched
> back to Tcl to develop a small graphical tool.
>
> I realized that my mind was already settled on C++/Qt OO way so I
> tried to get OO on Tcl too. I have already studyied a bit of XOTcl,
> but since 8.6b1 is out now, I decided to give it a chance. TclOO works
> great and I can use most of the XOTcl features I used too (not the
> most advanced ones, but that is ok).
>
> The problem comes when I try to mix a well designed OO system with Tk.
> Tk widgets are always global and callbacks (the ones you bind with -
> command) also need to be global. I found some interesting references,
> but the doubt still persists. The references are:
>
> http://wiki.tcl.tk/21103http://groups.google.com/group/comp.lang.tcl/browse_thread/thread/4de...
>
> My question is more general. Even before OO, I've never been satisfied
> that, after a well-built application, with correct function calling
> and encapsulation by scope, I had to deal with widgets declared
> globally. Since we can't just get rid or that feature, I'd like to
> ask: what are the practices you adopt when building your Tk
> applications? How would you layer your application in pure-Tcl core
> and Tk frontend?
>
> Thank you!

The Wiki has several pages on that very subject (model-view-controller
is the
technical term - http://wiki.tcl.tk/6225).

But a down-to-earth approach could be:
- Keep the set-up of your Tk widgets in dedicated procs
- Use callback procedures to add the functionality you need, do not
do:

button .b -text Exit -command {puts $::outfile [array get myState]\;
exit}

but:

button .b -text Exit -command SafeData

- Use explicit namespaces and namespace variables in them to keep
track
of the state of your application (both the internal logic and the
visual
aspect) instead of just global variables.

Regards,

Arjen