next up previous
Next: Building the interface Up: An introduction to Previous: Widget paths

Creating windows

So far we've only seen examples of using widgets in the main window of wish . But what if we want to create an application that has multiple windows? Then we need to create our own windows, called toplevels in Tk. A toplevel can be created like a widget:

% toplevel .t -width 320 -height 200 -text "A new window"
In contrast to a widget, a toplevel is shown directly after it is created, since there's no need to put it somewhere in a window.

An example of a window using a frame and a couple of buttons is created with the following commands:

toplevel .t
frame .t.f
button .t.f.b1 -text "Button 1" -command { puts "Button 1 pressed" }
button .t.f.b2 -text "Button 2" -command { puts "Button 2 pressed" }
pack .t.f.b1 -side top
pack .t.f.b2 -side bottom
pack .t.f -side left
button .t.b3 -text "Button 3" -command { puts "Button 3 pressed" }
pack .t.b3 -side right -fill both
wm title .t "A new window"
This creates a window with pathname .t. All widgets that are to be put in this window are prepended with this pathname. This example also demonstrates binding simple commands to buttons and changing the title of a window by means of the window manager command (wm). To change the title of a window, a command of the form wm title windowpath newtitle is given (see [1] for more information about wm-commands).

The resulting window is shown in figure 5.

  
Figure 5: Example of a toplevel



SE Praktikum
Tue Aug 13 13:33:30 MET DST 1996