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 variableitk_interiorcontains 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
Widgetclass is implemented with a Tk frame, mega-widgets in theWidgetclass can be packed into other frames and toplevels.
Example
Suppose we want to create aTextDisplaymega-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:
![]()
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::Widgetbase 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
displaymethod is used to set the contents of the text window, and theappendmethod 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 $itk_interior.info -state disabled \
-yscrollcommand [code $itk_interior.sbar set]
} {
usual
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 $itk_interior.info 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 theitk_initializemethod. Each derived class must invoke theitk_initializemethod 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:
- Default option settings for the mega-widget. These should be set with the "widgetDefault" priority.
- Declarations for "usual" option-handling code. This is a series of
keepandrenamestatements that says how the options should be handled if this mega-widget is included as a component of another mega-widget. It is usually a good idea to keep generic options like "-background" and "-font". Options like "-text" and "-command" that make each widget unique should be ignored for the "usual" set.
- A procedure with the same name as the mega-widget class, but with all lower-case letters, that can be used to create mega-widgets. In the example above, the "
textdisplay" command can be used to createTextDisplaymega-widgets. This adheres to the Tk convention for widget creation commands.