Geometry Managers

The geometry manager is responsible for determining where the widgets get placed inside the window. This section discusses the four basic geometry managers: pack, grid, place, and form.

pack

The basic algorithm of the pack geometry manager is to “shove the widgets together as closely as possible in the indicated direction.” There are four pack regions: top, bottom, left, and right.

The geometry manager must not only support the initial placement of the widgets, but also must redo the layout if the size of the window changes. The pack algorithm lets you anchor a widget to a point within the packing region.

This is done through the –anchor option. Figure 13.3 shows the values for this option.

Figure 13.3. Anchors.


In addition, the geometry manager manages the size of the widget. The –expand option controls whether the widget expands to fill the available space. In addition, the –fill option tells the geometry manager whether the widget is to fill the available space. The value of –fill can be "x", "y",“both",or "none".

You’ll see how these things work in a real example. Figure 13.4 shows the main window of the tk_grep.pl program. (Listing 13.2 shows the full program.)

Figure 13.4. tk_grep.pl.


You’ll see how to assemble this window. At the top is a "Pattern" blank. This is actually two widgets, a label (the Pattern text) and a blank (an Entry widget). You want this pair at the top of the window.

So you need a grouping widget. This widget is called a Frame. A Frame is a widget in which you put other widgets. In this case, you create a Frame and pack the Label and Entry widgets into it.

The Label is packed on the left side, the Entry on the right.

Now you need to consider expansion options. You don’t want the label to grow even if you resize the window, so it will not expand. The text entry can grow, however, but only in the x direction. (You don’t want it going any higher.)

The code for creating the top frame looks like the following:

my $pattern = "Text"; 
my $tk_frame = $tk_mw–>Frame(
    ); 
$tk_frame–>Label(
    –text => "Pattern" 
    )–>pack(
    –side => "left" 
    ); 
$tk_frame–>Entry(
    –textvariable => $pattern 
    )–>pack(
    –side => "right", 
    –fill => "x", 
    –expand => 1 
    );

Now you need to pack the frame as well. It will be packed against the top of the window, and it can expand in the x direction. So the pack command is

$tk_frame–>pack(
    –side => "top", 
    –fill => "x" 
);

This process is repeated for the Files blank.

For the two action buttons (Search, Cancel), simply pack them on the left and right sides.

Figure 13.5 shows the various widgets and their packing options.

Figure 13.5. Window packing options.


As you can see, with the pack geometry manager and a few frames you can make complex GUIs with a few simple commands.

grid

The grid geometry manager allows you to lay out your widgets in a set of rows and columns. It is not as flexible as the pack geometry manager, but it is useful in some cases.

The manager comes with a number of options and even lets you create grids with widgets that span rows and columns.

place

The place geometry manager lets you do all the work of deciding where you want your widgets yourself. In other words, you must explicitly place the widgets inside the window.

form

A newer geometry manager is the form. It gives you the capability to describe the location of a widget in a variety of ways. One way is to specify a location in terms of a percentage of the window. In other words, you can say “I want this widget to be put 30 percent below and 10 percent to the right of the upper-left corner.”

The form also lets you specify a widget’s location through attachments and springs, which gives you a way of placing your widget relative to other widgets.

Note that the form geometry manager is new and, as of this writing, not completely documented or implemented yet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset