Mod /
Tk Mod
Search:  


1.  Tk/Mod

Tk/Mod provides a refactored version of the standard Tk widgets. In Mod Tk widgets are redefined as namespace-modules. Each widget is a module-command, defined within the ::Tk namespace using it's class name. Thus text becomes ::Tk::Text. The sub-commands therein accept the Tk window-path as an argument. At runtime, Mod dynamically generates the subcommands and arguments, which it caches in ~/.tcl. (See Tk Widget Externs.)

Note: when using Mod, an explicit ::Tk prefix is not required as a uses is implied.

2.  Examples

Here is an example use of Tk/Mod:

package require Mod
Frame new .f
set t [Text new .f.t]
Text configure $t -height 10 -width 10
Text insert $t 1.0 "Hello World"
Text xview set $t

This is different from standard Tk as the widget path is just an argument. Also the sub-cmd new is used for creation. The advantages of using this Tk/Mod form is that it allows command completion using the Ted editor and command checking using Weld.

NOTE: There are several issues to be aware of with Tk/Mod such as sub-command shortforms should not be used (except conf). Another is that the programmer must ensure the correct window type is passed to a command. A runtime check for this is made if env(TCL_CHECK)==1, checking that window arg matches [winfo class].

3.  Sub-Command Extensions

New subcommands have been added to some or all widgets in order to provide more consistent or enhanced functionality.

3.1  conf

The conf or configure command sets options for a widget. Normally, errors are silently ignored. When env(TCL_CHECK) == 1, errors are reported as warnings. When env(TCL_CHECK) == 10, they become errors. Note that conf also supports itemconf, entryconf, etc. Note, conf is the only subcmd that is an officially supported as a shortform.

3.2  new

The new subcommand has been added to handle widget creation. When passed a window path with a trailing dot, it automatically generates and returns a new (unnamed) child path name. eg.

  set f [Toplevel new .t]
  set f1 [Frame new $f.]
  set t [Text new $f1.]

New also determines if a window is a named window and if so applies any applicable style (see below).

3.3  named

A named window path is defined as one whose tail part starts with an alphabetic and the rest containing only wordchars. Also, the window must have been created with the new sub-cmd. Toplevel provides the named sub-cmd to lookup these named windows, and a style sub-cmd apply attributes. For example all the following are named paths:

  Frame new $f.top
  Frame new $f.subwin
  Frame new $f.bot

and all the following are unnamed:

  Frame new $f._sub
  Frame new $f.9
  Frame new $f.

A list of all named windows associated with a toplevel can be returned by calling named with no argument.

3.4  style

The Toplevel command provides the style sub-cmd for specifying style informatoin that is to be applied to named windows therein, when using the new command. The form is vaguely like CSS in HTML. The style form consists of a list of selector/attribute-set pairs. eg.

  mainwin { -bg Blue  -fg Red }  subwin  { -bd 1  -relief raised }

Each selector in the style is of the form:

   winname - a named window
   Class - a window class
  @CMD - a name/val pairs lookup.

Attributes themselves are lists of name/value pairs where each name is prefixed by one of:

    "-" : A Tcl option.
    "*" : Use Tk DB, ie. [option add].
    "@" : Cmd call in stylecmds, eg @tip.
    "=" : expand a definition.

Here is an example:

 Toplevel style $x {
   defs        { -bg white -fg black }
   datawin   { = defs -cursor xterm }
   topframe { = defs *background yellow @tip "This is a tip" }
   mainwin   { -fg blue -bg red }
   Text        { -fg black -bg white }
   awin        { @font italic }
   mybut     { @image foo.gif }

See the style @cmds section for details on @tip, etc.

3.5  x/yview set

The xview and yview sub-commands have and added set sub-sub-command for setting/getting the current coords.

4.  New Widgets/Facilities

Mod adds the following meta-widgets/facilities to Tk. Note: these are not really well documented yet. See the code in the directory Mod/tcltk.

4.1  Tooltip

As the name implies, Tooltip provides popup balloon or tooltips. It is used by the @tip style.

4.2  Fonts

Provides management of named fonts in association with windows. When windows (and in particular toplevels) are destroyed all associated fonts are deleted.

The @font style uses the redefine sub-command which accepts a hybrid font specification somewhat like CSS wherein multiple families may be given. eg.

  Fonts redefine .w {"foobar,comic sans ms,verdana" italic}

In the above, the first family found on the system is used.

4.3  Images

Provides management of named images associated with windows. When windows (and in particular toplevels) are destroyed all associated images are deleted.

The [Images new] sub-cmd can accept options like -file, -data, etc. Use -opts to pass specific suboptions such as -width.

4.4  Labelframe

Provides a limited, minimal backward compatible labelframe for Tcl 8.3. Note: this is crufty and requires manual padding to use.

4.5  Menus

Provides creation of Menus based on an input description list. Uses a XTL? (Xml Tcl List) as input to describe the menu.

4.6  Notetab

The Notetab widget provides a scrollable notebook widget with an interface somewhat like ttk::notebook, but using buttons to control the selection of panes. Example uses may be seen in this screenshot, or with teds buffer list.

Following are some of the incompatibilities with ttk:

  • Tab labels do not shortened to fit available space.
  • [configure/cget] also accepts frame options.
  • [tab] also accepts options for button (but -compound is ignored with 8.3).
  • The -style and -sticky options are ignored.
  • The cmds [instate] and [state] just return 0 and {}.
  • The enableTraversal call is a subcmd, and should be called after creation

of tabs, or recalled after adding new tabs with -underline.

  • Control-Tab etc is unbound.

New features that are not part of ttk::notebook (as of 8.5a6):

  1. Option -scrolled uses auto-scrollbars when tabs wider than frame (creation only).
  2. Option -side for placing tab other than on top (creation only)
  3. Tab option -close for a close 'x' button to appear beside each tab.
  4. Tab buttons can each be in their own frame: see pc(framed).
  5. Widgets other than button may be use via -widcmd or pc(widcmd).
  6. Move next/prev tab using <Control-Prior/Next> (ie. ctl-pgdn/pgup)
  7. Shift-Tab can be used for traversal, spacebar to select.

Note: -scrolled default's to True, but is ignored when ::Tk::Scrolled isn't available (ie. if used without Mod).

Known Bugs:

  • using -scrolled with -close shows both scrollbars.
  • enableTraversal limits underline to valid bind chars (ie. alphanum).

4.7  Schemes

Schemes provides setting of color schemes by toplevel as well as named sets of color schemes.

4.8  Scrolled

The Scrolled widget simply provides scrollbars on demand for other widgets, particularly Text.

5.  Style Commands

Style commands begin with an ampersand @. Style commands may be used in two different ways, eg:

   mainframe { @tip "The main window" }
   subwin     { @tip "The sub-window" }

or

   @tip {
        mainframe "The main window"
        subwin "The sub-window"
    }

In the first form @tip is used as a name within a style value. In the second, @tip provides a table of window-name/value pairs. As with switch a value of '-' means use the following value.

Pre-defined style commands include the following:

5.1  @tip - a tooltip balloon

Provide a tooltip balloon msg when over a widget.

5.2  @font - font specification

Apply a font specification to the widget using Fonts redefine.

5.3  @fontdef - define a named font

Define a named font associated with widget using Fonts redefine.

5.4  @image - image to apply to element.

Apply a image specification to the widget using Images new.

5.5  @scheme - color scheme

Apply given color scheme to widget, which currently will affect the entire widget tree. The value can be any valid color, or any of the values listed the Ted menu /Preferences/Appearance/Color-Schemes, which includes: BeOS {Media Peach} {Dark Blue} {Desert red} {Digital CDE} Win2K {Point Reyes Green} Pumpkin Storm Solaris {Pale gray} {KDE 1} {Atlas green} EveX Bisque {Blue slate} Win95 CDE Keramik System WinXP Tk Next.

5.6  @conf - widget configuration list

Apply the configuration options to the widget.

5.7  @match - conditional style

Apply style if window name matches pattern.

5.8  @include - style file to inline.

Include the given style file inline.

5.9  @pack - pack widget

Invoke pack on the widget with the given values.

5.10  @place - place widget

Invoke place on the widget with the given values.

5.11  @grid - grid widget

Invoke grid on the widget with the given values.

5.12  @bind - apply key bindings

Apply name/value pair key bindings to window.

5.13  @tags - Text window tags

Define text window tag definitions using name/value pairs.

© 2008 Peter MacDonald

Page last modified on March 02, 2008, at 09:02 AM