Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 29.  Simple Tk Widgets

Frames and Toplevel Windows

Frames have been introduced before for use with the geometry managers. There is not much to a frame, except for its background color and border. You can also specify a colormap and visual type for a frame. Chapter 38 describes visual types and colormaps on page 540.

A toplevel widget is like a frame, except that it is created as a new main window. That is, it is not positioned inside the main window of the application. This is useful for dialog boxes, for example. A toplevel has the same attributes as a frame, plus screen and menu attributes. The menu attribute is used to create menubars along the top edge of a toplevel. This feature was added in Tk 8.0, and it is described on page 397. On UNIX, the screen option lets you put the toplevel on any X display. The value of the screen option has the following format:


For example, I have one X server on my workstation sage that controls two screens. My two screens are named sage:0.0 and sage:0.1. If the screenNum specifier is left off, it defaults to 0.

Attributes for Frames and Toplevels

Table 29-1 lists the attributes for the frame and toplevel widgets. The attributes are named according to their resource name, which includes a capital letter at internal word boundaries. When you specify an attribute in a Tcl command when creating or reconfiguring a widget, however, you specify the attribute with a dash and all lowercase letters. Chapter 28 explains how to use resource specifications for attributes. Chapters 37, 38, and 39 discuss many of these attributes in more detail.

Table 29-1. Attributes for frame and toplevel widgets.
backgroundBackground color (also bg).
borderWidthExtra space around the edge of the frame.
classResource class and binding class name.
colormapThe value is new or the name of a window.
containerIf true, frame embeds another application.
cursorCursor to display when mouse is over the frame.
heightHeight, in screen units.
highlightBackgroundFocus highlight color when widget does not have focus.
highlightColorFocus highlight color when widget has focus.
highlightThicknessThickness of focus highlight rectangle.
menuThe menu to use for the menubar. Toplevel only.
reliefflat, sunken, raised, groove, solid or ridge.
screenAn X display specification. (Toplevel only, and this cannot be specified in the resource database).
takeFocusControls focus changes from keyboard traversal.
useA window ID from winfo id. This embeds the frame or toplevel into the specified window.
visualType: staticgrey, greyscale, staticcolor, pseudocolor, directcolor, or truecolor.
widthWidth, in screen units.

You cannot change the class, colormap, visual, or screen attributes after the frame or toplevel has been created. These settings are so fundamental that you need to destroy the frame and start over if you must change them.

Embedding Other Applications

The container and use attributes support application embedding. Embedding puts another application's window into a Tk frame or puts a Tk frame into another application. The use attribute specifies the ID of a window that will contain a Tk frame. Wish supports a -use command line argument that is used for the same purpose. Set the container attribute if you want to embed another window. For example, here is how to run another wish application and embed its window in one of your frames:

frame .embed -container 1 -bd 4 -bg red
exec wish somescript.tcl -use [winfo id .embed] &

Toplevel Window Styles

On Windows and Macintosh there are several styles of toplevel windows. They differ in their appearance and their behavior. On UNIX, toplevel windows are usually decorated by the window manager, which is a separate application. Chapter 41 describes how to interact with the window manager.

On Macintosh, Tk has an unsupported1 command that you can use to set the window style:

unsupported1 style window style

The possible values for style include documentProc, dBoxProc, plainDBox, altDBoxProc, movableDBoxProc, zoomDocProc, rDocProc, floatProc, floatZoomProc, floatSideProc, or floatSideZoomProc. The dBoxProc, plainDBox, and altDBoxProc styles have no title bar, so there is no close box on them. The other styles have different title bars, a close box, and possibly a full-sized zoom box. The default style is documentProc. I used the following code to see what each looked like:

Example 29-1 Macintosh window styles.
set x {documentProc dBoxProc plainDBox altDBoxProc \
    movableDBoxProc zoomDocProc rDocProc floatProc \
    floatZoomProc floatSideProc floatSideZoomProc}
foreach y $x {
    toplevel .$y
    label .$y.l -text $y
    pack .$y.l -padx 40 -pady 20
    if [catch {unsupported1 style .$y $y}err] {
       puts "$y: $err"

This feature may appear as part of the wm command in future releases of Tk. On Windows you can get a couple different styles by using transient and overrideredirect windows, which are described on page 576.

      Previous section   Next section