How to Create GUI Application with Python and Tkinter

How to Create GUI Application with Python and Tkinter

The pack geometry manager

The pack manager can be a bit tricky to explain in words, and it can best be understood by playing with the code base. Fredrik Lundh, the author of Tkinter, asks us to imagine the root as an elastic sheet with a small opening at the center. The pack geometry manager makes a hole in the elastic sheet that is just large enough to hold the widget. The widget is placed along a given inner edge of the gap (the default is the top edge). It then repeats the process till all the widgets are accommodated.
Finally, when all the widgets have been packed in the elastic sheet, the geometry manager calculates the bounding box for all the widgets. It then makes the parent widget large enough to hold all the child widgets.
When packing the child widgets, the pack manager distinguishes between the
following three kinds of space:

• The unclaimed space
• The claimed but unused space
• The claimed and used space

The most commonly used options in pack include the following:

• side: LEFT, TOP, RIGHT, and BOTTOM (these decide the alignment of the widget)
• fill: X, Y, BOTH, and NONE (these decide whether the widget can grow in size)
• expand: Boolean values such as tkinter.YES/tkinter.NO, 1/0, True/False
• anchor: NW, N, NE, E, SE, S, SW, W, and CENTER (corresponding to the cardinal directions)
• Internal padding (ipadx and ipady) for the padding inside widgets and external padding (padx and pady), which all default to a value of zero

Let’s take a look at a demo code that illustrates some of the pack features.
Two of the most commonly used pack options are fill and expand.
The following is the code that generates the preceding GUI:

from tkinter import *
root = Tk()
frame = Frame(root)
# demo of side and fill options
Label(frame, text="Pack Demo of side and fill").pack()
Button(frame, text="A").pack(side=LEFT, fill=Y)
Button(frame, text="B").pack(side=TOP, fill=X)
Button(frame, text="C").pack(side=RIGHT, fill=NONE)
Button(frame, text="D").pack(side=TOP, fill=BOTH)
frame.pack()
# note the top frame does not expand nor does it fill in
# X or Y directions
# demo of expand options - best understood by expanding the root widget and seeing the effect on all the three buttons below.
Label (root, text="Pack Demo of expand").pack()
Button(root, text="I do not expand").pack()
Button(root, text="I do not fill x but I expand").pack(expand = 1)
Button(root, text="I fill x and expand").pack(fill=X, expand=1)
root.mainloop()

The following is a description of the preceding code:
• When you insert the A button in the root frame, it captures the leftmost area of the frame, expands, and fills the Y dimension. Because the fill option is specified as fill=Y, it claims all the area that it wants and fills the Y dimension of its frame container frame.
• Because frame is itself packed with a plain pack() method with no mention of a pack option, it takes the minimum space required to accommodate all of its child widgets.
• If you increase the size of the root window by pulling it down or sideways, you will see that the all the buttons within frame do not fill or expand with the root window.
• The positioning of the B, C, and D buttons occurs on the basis of the side and fill options specified for each of them.
• The next three buttons (after B, C, and D) demonstrate the use of the expand option. A value of expand=1 means that the button moves its place on resizing the window. Buttons with no explicit expand options stay at their place and do not respond to changes in the size of their parent container (the root window in this case).
• The best way to study this piece of code would be to resize the root window to see the effect that it has on various buttons.
• The anchor attribute (not used in the preceding code) provides a means to position a widget relative to a reference point. If the anchor attribute is not specified, the pack manager places the widget at the center of the available space or the packing box. The other options that are allowed include the four cardinal directions (N, S, E, and W) and a combination of any two directions.
Therefore, valid values for the anchor attribute are CENTER (the default value), N, S, E, W, NW, NE, SW, and SE.

To be continued…

Leave a Reply

Prev
Python CONDITIONALS Exercise for Beginners and Intermediate

Python CONDITIONALS Exercise for Beginners and Intermediate

Enable the edit mode with double click on the editor

Next
Extract PDF to Text File With Python
Extract PDF to Text File With Python

Extract PDF to Text File With Python

Today we are going to create a PDF to Text extractor

You May Also Like