Poet2.2 Manual Prototype Object Extension for Tcl

Tier 2

Tier 2 extends Tk, Ttk, and BWidgets and creates the objects necessary to construct new graphical user interface widgets. The ancestor to all widgets is ProtoWidget. Objects that are derived from the Tk/Ttk and BWidget widgets have been automatically generated (see Tier 2b) and are descendant from TkWidget and BWidget, respectively.

Each widget has slots corresponding to the widget's options with the same names and default values, so you can use the existing Tk, Ttk, and BWidget manual pages as a guide when creating the corresponding Poet objects. The construct method for ProtoWidget has been modified to take additional arguments. The first argument is the name of the new widget to create, followed by the name of the container or shell widget the new widget should be constructed in. This can either be a Tk pathname or a ProtoWidget. The remaining arguments are -slot value pairs that cause the named slots to be set to the given values, so creating a ProtoWidget ends up looking a lot like creating a Tk widget.

All of the slots derived from the Tk, Ttk, and BWidget options are write-active slots. When the slot value is changed, the option is set on the wrapped widget (which is stored in the slot _primary). ProtoWidgets also have a DEFAULT method that causes any unrecognized methods to be invoked as commands on the wrapped widget.

ProtoWidgets have an additional slot called layout that specifies the geometry manager and options to use when laying out the widget. If the value begins with -, it is assumed to be the arguments to pack, else the first word should be the name of the geometry manager (grid, place, etc.).

The appearance of Tk widgets and BWidgets are configured using their slots. The themed Ttk widgets available in Tcl/Tk 8.5, however, do not have individual options for their appearance attributes, instead they are configured via the ttk::style command and have a -style attribute (and a corresponding style slot in Poet). In many cases, you will not want to modify the styles of individual widgets, relying instead on the default theme, which gives you widgets that look native to the windowing system you're using. If you do use styles, set up your styles as you would in a regular Ttk application, but use Poet style in place of ttk::style when you operate on styles. Poet style behaves exactly like ttk::style, taking the same arguments and returning the same values, except that it maintains an internal list of all of the style names that have been created in your program. You can retrieve this list using the command Poet style names. When editing a Ttk widget using the Poetics object editor, the editor for the style slot will be a drop-down list of all of the custom styles for that class of widget. In summary, simply use Poet style instead of ttk::style everywhere in a Tcl/Tk 8.5 Poet application.

There are several widgets that have been written for tier 2 that can be used as examples for how to create your own widgets (tier 3 has even more examples). When defining a new widget, you must override ProtoWidget buildPrimary. This method will be invoked on your widget to construct its internal parts.

Note that, by creating a Poet object to mirror each Tk or BWidget widget, we can use constraints to express relationships between them. This is a complete example which creates a scale and a button in the . toplevel. Pressing the button resets the scale to 0. If the scale is 0, the button is automatically disabled.
package require Poet

Tk_Scale construct scl . -from -7 -to 7 -orient horizontal \
-layout {-side top}

Tk_Button construct btn . -text "Reset" -layout {-side top} \
-command "scl slot value 0"

btn formula state {
expr {[scl slot value] == 0 ? "disabled" : "normal"}
btn slotConstrain state
A widget demo, consisting mostly of translated versions of the Tk and Ttk demos, can be run with the command ProtoWidget demo. The demo is more interesting if tier 3 is enabled.