Version 9 of Model / View / Controller

Updated 2003-09-16 14:44:19

Model-View-Controller [L1 ] (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.

Some features of Tk operate in this way; notably the -variable option to entry, the -textvariable option to label, button et al, and the -listvariable option to listbox.

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.


See a tiny example at a little multiplication toy, a slightly bigger one at Revello


Category Concept | Category GUI