3.1 Toplevel Widgets and Widget Objects

A toplevel widget serves as the outermost container for other widgets. It can be created from the class Tk.toplevel and can be initialized by applying it to a message with label tkInit. An example is shown in Figure 3.1. The features width and height of the message together with their values specify that the toplevel widget is 150 pixels wide and 50 pixels high.

W={New Tk.toplevel tkInit(width:150 height:50)}

Figure 3.1: Creating a toplevel widget.

Creating and initializing a widget object creates a graphical image for the widget object. We will refer to the graphical image just as the widget. Most often we will not distinguish between the object and its widget. A toplevel widget is special in that its graphical image appears immediately on the screen after the widget object has been initialized. Other widgets require to be managed by a so-called geometry manager before they appear on the screen. See Chapter 4 for a discussion of geometry managers.

A widget object can be sent a message with label tk to change the appearance or behavior of its widget. For example, the background color of the toplevel widget W can be changed to purple by

{W tk(configure background:purple)}

Additionally, a widget object understands messages that query its state. These will be discussed later.

A widget object can be closed by applying the object to the message tkClose. Closing the widget object also destroys the widget displayed on the screen. Section 3.4 contains more details concerning how widget objects are closed.

The structure of messages with labels tkInit and tk depend on the particular widget under consideration. However, all of these messages share a common structure. The following section explains this structure and shows how to build messages such that they are understood by widgets.

Reference information on toplevel widgets can be found in toplevel.

Christian Schulte
Version 1.4.0 (20080702)