Tkinter pack layout manager with option fill side expand anchor to place widgets in different places
Understanding Pack
Without any options all widgets will be placed vertically in a column. We can add side , fill and expand to manage the layout.
side
side=LEFT
Placed at LEFT of Next element
side=RIGHT
Placed at RIGHT of Next element
side=BOTTOM
Placed at BOTTOM of next element
side=TOP
Placed at TOP of next element
side=left
import tkinter as tk
from tkinter import *
my_w=tk.Tk()
# Placed at LEFT of Next element b1
l1 = tk.Listbox(my_w,height=3,bg='yellow')
l1.pack(side=LEFT)
# Immaterail as no next element
b1=tk.Button(my_w,text='b1')
b1.pack(side=RIGHT)
my_w.mainloop()
side=LEFT ( three elements )
# Placed at LEFT of next b1
l1 = tk.Listbox(my_w,height=3,bg='yellow')
l1.pack(side=LEFT)
# Placed at LEFT of Next element b2
b1=tk.Button(my_w,text='b1')
b1.pack(side=LEFT)
b2=tk.Button(my_w,text='b2')
b2.pack(side=RIGHT) # Immaterial
my_w.mainloop()
side=RIGHT
l1 = tk.Listbox(my_w,height=3,bg='yellow')
l1.pack(side=LEFT)
# Next element will be placed RIGHT of this l1
b1=tk.Button(my_w,text='b1')
b1.pack(side=RIGHT)
# Next element ( b2 ) will be left of this b1
b2=tk.Button(my_w,text='b2')
b2.pack(side=RIGHT)
# Next element ( not there) so Immaterial
my_w.mainloop()
side=BOTTOM
# Next element will be placed RIGHT of this l1
l1 = tk.Listbox(my_w,height=3,bg='yellow')
l1.pack(side=LEFT)
b1=tk.Button(my_w,text='b1')
b1.pack(side=BOTTOM)
#Takes Next row relative to next element ( b2 )
b2=tk.Button(my_w,text='b2')
b2.pack(side=RIGHT) # Immaterial
#b2 will be above b1
fill
fill=X
Expand horizontally to fill the available space
fill=Y
Expand vertically to fill the available space
fill=BOTH
Expand horizontally & vertically to fill the available space
fill=X
import tkinter as tk
my_w=tk.Tk()
b1=tk.Button(my_w,text="Welcome",width=20)
b1.pack() # Occupies the assigned width
# in the absence of any side value for b1, b2 starts in next row
b2=tk.Button(my_w,text="b2")
b2.pack() # Width is as minimum, no expansion
b3=tk.Button(my_w,text="b3 with fill=X")
b3.pack(fill=X) # fill all horizontal space
my_w.mainloop()
fill=Y
l1 = tk.Listbox(my_w,height=3,bg='yellow')
l1.pack(side=LEFT) # Occupies the assigned width
# in the absence of any side value for b1, b2 starts in next row
b2=tk.Button(my_w,text="b2")
b2.pack(side=LEFT) # Width is as minimum, no expansion
b3=tk.Button(my_w,text="b3")
b3.pack(side=LEFT,fill=Y) # fill all vertical space
my_w.mainloop()
expand
Default value is False.
The expand option ( if True or 1 ) tells the manager to assign additional space to the widget box. If the parent widget is made larger than necessary to hold all packed widgets, any exceeding space will be distributed among all widgets that have the expand option set to a non-zero value.
Difference between expand and fill
expand option ( if set to True ) tells to take any not assigned space .
The option fill asks the widget to take as much space as possible in given direction.
Align at top left
Three side by side buttons are aligned at top left corner of the window
By using pack_forget() we can remove the widget from layout. The widget is not destroyed so we can restore the same. Here is a code to display and hide the message on button click. On click of button b1 the code command=lambda: l1.pack_forget() removes the widget. We can restore it by using pack() command=lambda: l1.pack().
We can delete the widget by using delete(). command=lambda: l1.destroy()
We have seen above about fill and side. Other options are listed here. ( Check pack_info() above to get dictionary of options with values print(b1.pack_info()) )
in : to pack widget inside
ipadx : internal horizontal padding , default is 0
ipady : internal vertical padding , default is 0
padx : External horizontal padding , default is 0
pady : External vertical padding , default is 0
side : which side the widget is to be packed
Here is an example of using these values
import tkinter as tk
from tkinter import *
my_w=Tk()
l1 = tk.Listbox(my_w,height=3,bg='yellow')
l1.pack(side=TOP) #
b1=tk.Button(my_w,text='RIGHT')
b1.pack(side=RIGHT,anchor='center',expand=1,fill=Y,
ipadx=10,ipady=10,padx=10,pady=10) # set the values
print(b1.pack_info()) # reading the values
my_w.mainloop()