To pack or to grid?

Using pack versus grid in your application is mostly down to personal preference. There doesn't seem to be a particularly dominant reason to use one over the other.

The main advantage of pack is the code tends to be very readable. pack uses words such as left and top to make it clear where the programmer wants the widget to go.

When using pack, sections of the window are also split using frames to allow for much greater control. When variables are named sensibly, this allows anyone changing the code to know exactly which part of a window the widget will end up in (by its parent Frame) and prevents them from having unexpected consequences when changing widgets, such as resizing a widget in the top-left corner of an application, knocking a widget at the bottom out of alignment.

The grid can also take advantage of Frame widgets too, but this can sometimes cause alignment issues.

Finally, pack works out widget positions based mainly on the argument and the order in which they are added. This means that when a new widget is added among existing ones, it is usually quite easy to get it into the correct spot. Simply adjust the order in which your widget.pack() calls occur. When using grid, you may need to change quite a few row and column arguments in order to slot the widget where you need it and keep everything else in their correct positions.

The great advantage of grid is its code simplicity to layout complexity ratio. Without the need to split your application into frames, you can save many lines of code and lay out a complicated window design with essentially just one line of code per widget.

You also don't need to worry about the order in which you add your widgets to their parent as the numerical grid system will apply regardless.

In the end, both prove to be good tools for the job and there is no need to use one if you prefer the other.

My personal preference is to use pack for main windows which may change quite a bit during development, and sometimes grid for smaller windows or layouts which are written in one go. Any additional windows for an application which would require more than two Frame widget are often better off being managed by grid for simplicity's sake.

Examples in this book will cover both grid and pack, so you will be able to practice both and decide which you prefer.

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

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