The Widget class inherits everything from the Archetype class, and adds a Tk frame called the "hull" component to represent the body of the mega-widget. The window class name for the hull is set to the most-specific class name for the mega-widget. The protected variable itk_interior contains the window path name for the "hull" component. Derived classes specialize this widget by packing other widget components into the hull.

Since the hull for the Widget class is implemented with a Tk frame, mega-widgets in the Widget class can be packed into other frames and toplevels.


Suppose we want to create a TextDisplay mega-widget class that provides a text widget and a scrollbar, and displays read-only text. If we had such a widget, we might use it like this:

    textdisplay .file -background red
    pack .file

    .file display [exec cat /etc/passwd]
and it would look like this:

screen snapshot of TextDisplay widget

Each time a new widget is created, the constructor is invoked automatically to create the internal component widgets. The "hull" component is created automatically by the itk::Widget base class. Other components can be packed into this. Each component must be created using the "itk_component add" method, as shown below. This method creates a component widget, gives it a symbolic name, and merges its options into the composite list for the mega-widget. Once a component has been created like this, its window path name can be referenced via its symbolic name as "$itk_component(name)".

The methods in the mega-widget class define the operations that the widget will respond to. In this case, the display method is used to set the contents of the text window, and the append method will add information to the current display.

option add *TextDisplay.wrap none widgetDefault
option add *TextDisplay.textBackground ivory widgetDefault
option add *TextDisplay.width 40 widgetDefault
option add *TextDisplay.height 10 widgetDefault

class TextDisplay {
    inherit itk::Widget

    constructor {args} {
        itk_component add text {
            text $ -state disabled \
                -yscrollcommand [code $itk_interior.sbar set]
        } {
            keep -tabs -wrap -width -height
            rename -background -textbackground textBackground Background
        pack $itk_component(text) -side left -expand yes -fill both

        itk_component add scrollbar {
            scrollbar $itk_interior.sbar \
                -command [code $ yview]
        pack $itk_component(scrollbar) -side right -fill y

        eval itk_initialize $args

    public method display {info}
    public method append {info}

body TextDisplay::display {info} {
    $itk_component(text) configure -state normal
    $itk_component(text) delete 1.0 end
    $itk_component(text) insert 1.0 $info
    $itk_component(text) configure -state disabled

body TextDisplay::append {info} {
    $itk_component(text) configure -state normal
    $itk_component(text) insert end $info
    $itk_component(text) configure -state disabled

usual TextDisplay {
    keep -background -cursor -foreground -font
    keep -activebackground -activerelief
    keep -highlightcolor -highlightthickness
    keep -insertbackground -insertborderwidth -insertwidth
    keep -insertontime -insertofftime
    keep -selectbackground -selectborderwidth -selectforeground
    keep -textbackground -troughcolor

proc textdisplay {pathName args} {
    uplevel TextDisplay $pathName $args

Notice how the constructor takes widget configuration options as an "args" list, and passes these arguments to the itk_initialize method. Each derived class must invoke the itk_initialize method in this manner within its constructor , so that all options are properly integrated and initialized in the composite list.

It is a good idea to include the following things along with the mega-widget class definition: