Tkinter Pack

Tkinter pack geometry manager is the easiest of all three geometry managers. It packs the widgets inside the parent window. we can use the expand, fill, side options to control the geometry of the widgets.

This geometry manager is only ideal for small and simple applications. for complex applications, we recommend using grid and place geometry managers.

Let’s look at the description of all options that we can use to control the geometry of widgets.

Tkinter Pack Options :

anchorSet the position of the widget inside the packing box. Use N, NE, E, SE, S, SW, W, NW, or CENTER(default).
expandUse this option to specify whether a widget should be expanded to fill extra space. default is False.
fillUse this option to specify whether a widget should occupy all the space. use None(default), X(fill horizontally), Y(fill vertically), BOTH (fill both horizontally and vertically).
ipadxSet the internal horizontal padding. default is 0.
ipadySet the internal vertical padding. default is 0.
padxSet the horizontal padding between widget and parent. default is 0.
padySet the vertical padding between widget and parent. default is 0.
sideSet the side to pack the widget. use TOP, LEFT, RIGHT, BOTTOM.

Tkinter Pack Examples

For a better understanding of the concepts. let’s look at some code examples.

Note: To able to understand these examples I assume that you have some basic knowledge about Tkinter buttons.

Tkinter Pack Example 1

In the following code example, we will first create two buttons and then display them on the window by using the pack() method.

from tkinter import *

window = Tk()

b1 = Button(window,text = "Button 1",fg = "white",bg="red")
b2 = Button(window,text = "Button 2",fg = "white",bg="blue")

# Simply pack widgets into window
b1.pack()
b2.pack()

window.mainloop()

Output :

Tkinter pack example 1 output

Example 2

In this code example, we will set the fill option to X to occupy all the horizontal space around the widget.

from tkinter import *

window = Tk()

b1 = Button(window,text = "Button 1",fg = "white",bg="red")
b2 = Button(window,text = "Button 2",fg = "white",bg="blue")
b3 = Button(window,text = "Button 3",fg = "white",bg="green")


#Fill all the buttons horizontally
b1.pack(fill = X)
b2.pack(fill = X)
b3.pack(fill = X)

window.mainloop()

Output :

Tkinter pack example 2 output

Example 3

In the following code example, we will set the fill option to BOTH and expand option to True.

from tkinter import *

window = Tk()

b1 = Button(window,text = "Button 1",fg = "white",bg="red")
b2 = Button(window,text = "Button 2",fg = "white",bg="blue")
b3 = Button(window,text = "Button 3",fg = "white",bg="green")


#Fill both horizontally and vertically. and expand it
b1.pack(fill = BOTH,expand = True)
b2.pack(fill = BOTH,expand = True)
b3.pack(fill = BOTH,expand = True)

window.mainloop()

Output :

Tkinter pack example 3 output

Example 4

In the below code example, we will use the side option to align all the buttons on different sides of the window.

from tkinter import *

window = Tk()

b1 = Button(window,text = "Button 1",fg = "white",bg="red")
b2 = Button(window,text = "Button 2",fg = "white",bg="blue")
b3 = Button(window,text = "Button 3",fg = "white",bg="green")
b4 = Button(window,text = "Button 4",fg = "white",bg="black")


b1.pack(side = TOP)
b2.pack(side = BOTTOM)
b3.pack(side = LEFT)
b4.pack(side = RIGHT)

window.mainloop()

Output :

Tkinter pack example 4 output

Example 5

In the following code example, we will use the side option to align all the buttons to the left.

from tkinter import *

window = Tk()

b1 = Button(window,text = "Button 1",fg = "white",bg="red")
b2 = Button(window,text = "Button 2",fg = "white",bg="blue")
b3 = Button(window,text = "Button 3",fg = "white",bg="green")



b1.pack(side = LEFT)
b2.pack(side = LEFT)
b3.pack(side = LEFT)

window.mainloop()

Output :

example 5 output

Example 6

In the below code, we will use the ipadx and ipady options to add some internal padding to the widgets.

from tkinter import *

window = Tk()

b1 = Button(window,text = "Button 1",fg = "white",bg="red")
b2 = Button(window,text = "Button 2",fg = "white",bg="blue")
b3 = Button(window,text = "Button 3",fg = "white",bg="green")

b1.pack(ipadx = 10,ipady = 10)
b2.pack(ipadx = 10,ipady = 10)
b3.pack(ipadx = 10,ipady = 10)

window.mainloop()

Output :

example 6 output

Example 7

In this final code example, we will use the padx and pady options to add some external padding between widget and parent.

from tkinter import *

window = Tk()

b1 = Button(window,text = "Button 1",fg = "white",bg="red")
b2 = Button(window,text = "Button 2",fg = "white",bg="blue")
b3 = Button(window,text = "Button 3",fg = "white",bg="green")



b1.pack(padx = 10,pady = 10)
b2.pack(padx = 10,pady = 10)
b3.pack(padx = 10,pady = 10)

window.mainloop()

Output :

example 7 output

That’s wrap for this tutorial.

Leave a Reply