Python Series Part 14 - Understanding Pack in Tkinter

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
454
Reaction score
520
Credits
19,511
Now that we’ve made a window, we need to populate it with objects, which are widgets in Python.

The problem with placing items in the window is that there are three methods to do so. Let’s look at the first one.

Pack

Pack is a method that allows an object to be placed in the window one line at a time, starting at the top (default). The items are centered in the window, as shown in Figure 1.

Figure 1.JPG

FIGURE 1

The code I used was just to place four labels in the window:

Code:
from tkinter import *

root = Tk()
root.title("Pack Example")
root.geometry("300x500")

l1 = Label(text='Label 1').pack()
l2 = Label(text='Label 2').pack()
l3 = Label(text='Label 3').pack()
l4 = Label(text='Label 4').pack()

root.mainloop()

I know we have covered none of the objects yet, but we’ll glance at the labels.

A ‘label’ is a changeable text area, but usually it is not. The objects are usually placeholders for some type of text.

Here, I am giving each label a different name (l1, l2, l3 and l4). The text for each label is just a label name and successive number. After we set up the initial basic label, we specify it is being used as a ‘pack’ layout.

We can specify the ‘pack’ as a separate command:

Code:
l1 = Label(text='Label 1')
l1.pack()

Either way, should work and any parameters we use for ‘pack’ can be in the parenthesis after the command ‘pack’.

Pack - Side

Initially, you can see that the default method of packing in the objects is from top to bottom. We can change this. The four directional parameters are:

  1. TOP (default) - top to bottom
  2. BOTTOM - bottom to top
  3. LEFT - left to right
  4. RIGHT - right to left
Let’s look at the ‘right to left’ option as an example:

Code:
l1 = Label(text='Label 1').pack(side=RIGHT)
l2 = Label(text='Label 2').pack(side=RIGHT)
l3 = Label(text='Label 3').pack(side=RIGHT)
l4 = Label(text='Label 4').pack(side=RIGHT)

Figure 2 shows the example. No matter which side you choose, the first object starts on the side specified and the rest follows to the opposite side.

Figure 2.JPG

FIGURE 2

Pack - Expand


So far, the widgets are only as big as the text within the label, in the previous example.

By default, the label is only as large as the text and not spread out. The best way to think of the widgets is as ‘cells’ as in a spreadsheet. Each cell is only as large as the text within it. The ‘cells’ are right next to each other since the size of the widget is both vertical and horizontal. With the ‘expand’ parameter, we can space the ‘cell’ out to have more space between itself and the next cell.

The ‘expand’ parameter works more like having a setting similar to ‘justified’ or ‘centering’ on spacing. We can center or not center each widget.

Let’s look at an example of centering only label 4:

Code:
from tkinter import *

root = Tk()
root.title("Pack Expand Example")
root.geometry("300x500")

l1 = Label(text='Label 1').pack(expand=False)
l2 = Label(text='Label 2').pack(expand=False)
l3 = Label(text='Label 3').pack(expand=False)
l4 = Label(text='Label 4').pack(expand=True)

root.mainloop()

You can see the output in Figure 3.

Figure 3.JPG

FIGURE 3

If you set a ‘side’, then the spacing will be up and down when using ‘top’ or ‘bottom’, while ‘left’ or ‘right’ will be spaced to the sides.

If you set all widgets to ‘True’, then the spacing of all widgets are even from each other.

Pack - Fill

We can change the cell to fill a specific amount of space depending on what type you choose. To make this work properly, you need to ‘expand’ the cell so it has room to fill the area specified.

Another option is to add color to allow the entire space to fill and be more noticeable. There are four types of ‘fill’:

  1. None (default) - no extra space is filled
  2. X - space to the left and right, the X axis, is filled
  3. Y - space above and below, the Y axis, is filled
  4. Both - space on the X axis and Y axis is filled
The following code shows how the ‘fill’ parameter works and is in Figure 4

Code:
from tkinter import *

root = Tk()
root.title("Pack Fill Example")
root.geometry("300x500")

l1 = Label(text='Label 1',fg='Blue',bg='white').pack(expand=TRUE,fill=None)
l2 = Label(text='Label 2',fg='Blue',bg='white').pack(expand=TRUE,fill=X)
l3 = Label(text='Label 3',fg='Blue',bg='white').pack(expand=TRUE,fill=Y)
l4 = Label(text='Label 4',fg='Blue',bg='white').pack(expand=TRUE,fill=BOTH)

root.mainloop()

Figure 4.JPG

FIGURE 4

Pack - IPADX and IPADY


Internal Padding (IPAD) to the left and right, or X axis, is IPADX while up and down, Y axis is IPADY.

Here, we can set a certain amount of padding to either axis, both or neither.

The code is:

Code:
from tkinter import *

root = Tk()
root.title("Pack IPADX/Y Example")
root.geometry("300x500")

l1 = Label(text='Label 1',bg='white').pack(expand=TRUE)
l2 = Label(text='Label 2',bg='red').pack(expand=TRUE,ipadx=25)
l3 = Label(text='Label 3',bg='blue').pack(expand=TRUE,ipady=25)
l4 = Label(text='Label 4',bg='purple').pack(expand=TRUE,ipadx=40,ipady=40)

root.mainloop()

Figure 5 shows the output.

Figure 5.JPG

FIGURE 5

We set the padding in pixels. To show the difference, I changed the background color of the labels to show how the cells are changed by the parameter.

PADX and PADY

With this parameter, you can set a spacing between cells.

PADX can set a horizontal spacing between cells when setting the side to RIGHT or LEFT. PADY is used to space cells vertically when using the TOP or BOTTOM side.

The code we can use is:

Code:
from tkinter import *

root = Tk()
root.title("Pack PAD Example")
root.geometry("400x500")

l1 = Label(text='Label 1',bg='blue').pack(side=RIGHT)
l2 = Label(text='Label 2',bg='blue').pack(side=RIGHT,padx=20)
l3 = Label(text='Label 3',bg='blue').pack(side=RIGHT,padx=30)
l4 = Label(text='Label 4',bg='blue').pack(side=RIGHT,padx=50)

root.mainloop()

Here, we do not change the cell size. Each cell is the width of the text it contains. There is a varying space between labels, as you can see in Figure 6.

Figure 6.JPG

FIGURE 6

The parameter works the same for PADX when setting the side to TOP or BOTTOM. You can change ‘RIGHT’ to ‘BOTTOM’ and ‘padx’ to ‘pady’ to see how it works.

Anchor

Using anchor, we can set a position for a widget that remains a constant, even when the user resizes the window.

The positions are based on the compass and there are nine positions:

  1. n - North (top center)
  2. s - South (bottom center)
  3. e - East (right center)
  4. w - West (left center)
  5. ne - North East (top right center)
  6. nw - North West (top left center)
  7. se - South East (bottom right center)
  8. sw - South West (bottom left center)
  9. center - center of the screen
The code is:

Code:
from tkinter import *

root = Tk()
root.title('Pack Anchor')
root.geometry("400x500")

l1 = Label(text="NW").pack(anchor=NW, expand=True)
l2 = Label(text="North").pack(anchor=N, expand=True)
l3 = Label(text="NE").pack(anchor=NE, expand=True)
l4 = Label(text="West").pack(anchor=W, expand=True)
l5 = Label(text="Center").pack(anchor=CENTER, expand=True)
l6 = Label(text="East").pack(anchor=E, expand=True)
l7 = Label(text="SW").pack(anchor=SW, expand=True)
l8 = Label(text="South").pack(anchor=S, expand=True)
l9 = Label(text="SE").pack(anchor=SE, expand=True)

root.mainloop()

As you can see, I put the items in order from left to right and from top to bottom. I did this since we packed each ‘cell’ into the window. If I did a different order, then the label placement would not be proper. The placement is one ‘cell’ per line. The output is shown in Figure 7.

Figure 7.JPG

FIGURE 7

The same occurs if I use ‘side=bottom’. If I use left or right, then no two cells can be in the same column.

Conclusion

This is one method that allows the placement of objects in a window. It is not the easiest since there are issues, such as seen with ‘anchor’.

There are two other methods that we can use, so we will look at these next.
 


Follow Linux.org

Members online


Top