Model-View-Controller [1 ] (often abbreviated "MVC") is a way of working with GUIs that provide access to complex data, and it seems to scale really quite well indeed. You do it by separating the code to implement the Model (your underlying data is, say, an updatable list or a read-only tree) from the View (how your data looks when drawn on the screen) and the Controller (how your model responds to updates to the view) though often the view and controller are joined together.
It's wise to design the rest of your interface that way as well. The trace command (particularly [trace variable]) is very useful for implementing triggers that update a view when the underlying model changes. The bindtags command is useful for associating a widget with one or more controllers other than the standard ones that Tk provides; occasionally, you also need to resort to overloading a widget command. For a worked example, see the page entitled, "Text variable for text widget". In that example, the widget command is overloaded to notify the model when the user attempts to change the text contents. A trace notifies the view when the model is updated. A bind tag is also used so that everything gets cleaned up correctly when the window is destroyed.
MVC is starting to accumulate heterodoxy, including "MVC considered harmful" [2 ], ...
EKB That's an interesting article. It's about an alternative approach called "context-oriented programming". I made a short example on ContexTcl: Playing with Context-oriented Programming.
PYK 2014-05-20: One possibly-useful guideline is that what to display belongs in the model or the controller , and how to display it belongs in the view . This guideline helps keep program logic from creeping into the view .
EKB One way to use the MVC pattern that I apply frequently is to make a core program with programmatic hooks and no user interface elements at all. After making it, I build a command-line program that sources a script that accesses the model. Basically, this means using it as a mini-language, which I think is a particularly Tclish (and Lispish) thing to do. Often the development stops there, because it's all I need, but it doesn't have to. Because it uses the MVC pattern, it would be easy to make a Tk interface that makes use of the core program. There's an example at Mini-language as Model-View-Controller.
Lars H: Hmm... As used above, "mini-language" seems almost a synonym of "package" (although perhaps not registered via the package mechanism). Is there some other distinction between the two that you feel is noteworthy but perhaps not immediately obvious, or is it just a matter of terminology? One distinction could be that a package is often set up to support multiple clients (if it keeps state, then the client might need to provide a handle to the particular context to use) whereas a mini-language has a global context shared by all (at least in that interpreter), but that could just as well be irrelevant.
EKB Not in terms of the code, but in terms of presentation and use, yes. On one hand I find that if I think about a package as a mini-language then I implement the package and the application somewhat differently. On the other hand, I find that exposing a package as a mini-language (or "domain language") and then presenting it to users that way, without ever mentioning Tcl, leaves them very impressed. It's still good advertising for Tcl/Tk, because later when they ask about it, I say, "Oh, yes, that's quite straightforward in Tcl." This ties into two things that I see in software development discussions: a) the use of domain languages as a strategy (highly recommended in the popular "Pragmatic Programmers" books); b) Lisp as the natural platform for making mini-languages (although not by the Pragmatic Programmers -- they like Ruby). But Tcl is also a natural platform for it. As I was thinking about the mini-language code I've written, I realized that it might be possible to add a (c) to this list, that a mini-language is a natural way to implement MVC that is (IMHO), more transparent and easier to implement than implementing MVC in, say, Java.
So, the crass view of saying "yeah, it's just a package, but..." is that it's a way to do some Tcl evangelism using some popular concepts: domain language and MVC. Something like, "TCL can implement MVC via a domain language at a lower TCO than alternatives and with lower barriers to entry." That sort of thing. The non-crass view of it is that thinking about an existing strategy in a new way affects my overall development process.
I am no expert, but when I write a GUI app I structure in a sort of "Micky Mouse" MVC (MMMVC?) I structure it the following way, both mentally and in terms of laying out the code (e.g., in the program text I set off these sections clearly with comments.)
I will post an example someday.
I look forward to comments!