Chapter 2 - Geometry ManagementTo display widgets on the screen, they must be passed to a geometry manager. The geometry manager controls the position and size of the widgets in the display window. There are several geometry managers available with Perl/Tk: pack, place, and grid.All three geometry managers are invoked as methods on the widget, but they all have their own methodologies and arguments to change where and how the widgets are put on the screen:$widget1->pack(); $widget2->place(); $widget3->grid();When you organize the widgets in your window, it is often necessary to separate groups of widgets to get a certain look and feel. For instance, when you use pack(), it is difficult to have widgets stacked both horizontally and vertically without grouping them in some fashion. We group widgets by using a frame widget inside a window or by using another window (a toplevel widget).We create our first window by calling MainWindow. The MainWindow is a special form of a toplevel widget. For more detailed information on how to create/configure frames and toplevel widgets, see Chapter 12, Frames, and Chapter 13, Toplevel Widgets.Because of the differences between the three geometry managers, it is difficult (not entirely impossible, but definitely not recommended) to use more than one geometry manager within the same area. In our $mw, I can display many types of widgets, but if I start using pack(), I should continue to use pack() on all of the widgets contained directly in $mw. I wouldn't want to switch in the middle to using grid(). Because a window can contain a frame, which in turn contains other widgets, we use pack() to pack the frame inside the main window and then we could use grid() to manage the widgets inside the frame.Although the different geometry managers have their own strengths and weaknesses, the most commonly used is pack(), so I'll discuss it first and in the most detail. The grid() geometry manager was under development as I was writing this book. grid has been improved greatly with the release of Tk 8.0 and subsequent porting to Perl. The place() geometry manager is the most tedious to use because you have to determine exact coordinates for every single widget.PackRemember when you were a small child and you had those wooden puzzles to put together? They often had cute little pictures of animals on them. Each piece in the puzzle had exactly one place where it could go, and there weren't any overlaps allowed between pieces.With the pack geometry manager, our windows are similar to the wooden puzzle because widgets cannot overlap or cover each other (partially or completely). See Figure 2-2. If a button is packed in a certain space on the window, the next button (or any widget) will have to move around the already packed button. Luckily, our windows will only be dealing with rectangular shapes instead of funny-shaped puzzle pieces.The order in which you pack your widgets is very important because it directly affects what you see on the screen. Each frame or toplevel maintains a list of items that are displayed within it. This list has an order to it; if widget A is packed before widget B, then widget A will get preference. This will become clear as we go through some examples. You will often get a very different look to your window just by packing the widgets in a different order.If you don't care what the window looks like and how the widgets are put in it, you can use pack() with no arguments and skip the rest of this chapter. Here it is again:$widget->pack();To make your window look nicer and more manageable (and user friendly), there are arguments that can be sent to the pack method that will change the way the widgets and the window looks. As with anything in Perl/Tk, the arguments are arranged in pairs. So the more sophisticated usage would be:$widget->pack( [ option => value, ... ] );
ASIN : 1565923146
Binding : Paperback
EAN : 9781565923140
Edition : 1
Feature :
ISBN : 1565923146
Label : O'Reilly Media
Languages :
Manufacturer : O'Reilly Media
Number of Pages : 373
Publication Date : 8 Jan 1999
SKU : 1002268095
Studio : O'Reilly Media