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 |
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)
# Immaterial as no next element
b1=tk.Button(my_w,text='b1')
b1.pack(side=RIGHT)
my_w.mainloop()
# 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()
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()
# 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=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 |
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()
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()
This example demonstrates the usage of fill=BOTH to expand the button both horizontally and vertically, occupying all available space.
import tkinter as tk
my_w = tk.Tk()
l1 = tk.Listbox(my_w, height=3, bg='yellow')
l1.pack(side=tk.LEFT)
b2 = tk.Button(my_w, text="b2")
b2.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
b3 = tk.Button(my_w, text="b3")
b3.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
my_w.mainloop()
Here, both buttons b2 and b3 fill both horizontal and vertical space due to the fill=BOTH and expand=True options.
Three side by side buttons are aligned at top left corner of the window
b1=tk.Button(my_w,bg='yellow',text='B1')
b1.pack(side='left',anchor='nw')
b2=tk.Button(my_w,bg='lightblue',text='B2')
b2.pack(side='left',anchor='nw')
b3=tk.Button(my_w,bg='yellow',text='B3')
b3.pack(side='left',anchor='nw')
b1=tk.Button(my_w,bg='yellow',text='B1')
b1.pack(side='right',anchor='ne')
b2=tk.Button(my_w,bg='lightblue',text='B2')
b2.pack(side='right',anchor='ne')
b3=tk.Button(my_w,bg='yellow',text='B3')
b3.pack(side='right',anchor='ne')
b1=tk.Button(my_w,bg='yellow',text='B1')
b1.pack(side='right',anchor='se')
b2=tk.Button(my_w,bg='lightblue',text='B2')
b2.pack(side='right',anchor='se')
b3=tk.Button(my_w,bg='yellow',text='B3')
b3.pack(side='right',anchor='se')
b1=tk.Button(my_w,bg='yellow',text='B1',font=font1)
b1.pack(side='left',anchor='sw')
b2=tk.Button(my_w,bg='lightblue',text='B2',font=font1)
b2.pack(side='left',anchor='sw')
b3=tk.Button(my_w,bg='yellow',text='B3',font=font1)
b3.pack(side='left',anchor='sw')
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(). command=lambda: l1.destroy()
import tkinter as tk
from tkinter import *
my_w=tk.Tk()
my_w.geometry("220x150")
my_str = tk.StringVar(value='Hi welcome')
l1 = tk.Label(my_w, textvariable=my_str, width=20 )
l1.pack(side=TOP)
b1=tk.Button(my_w,text='Remove',
command=lambda: l1.pack_forget() )
b1.pack(side=LEFT)
b2=tk.Button(my_w,text='Display',
command=lambda: l1.pack())
b2.pack(side=LEFT)
b3=tk.Button(my_w,text='Destroy',
command=lambda: l1.destroy())
b3.pack(side=LEFT)
my_w.mainloop()
print(b1.pack_info())
Output , option names with values as key value pairs of a dictionary.
{'in': , 'anchor': 'center', 'expand': 0, 'fill': 'none',
'ipadx': 0, 'ipady': 0, 'padx': 0, 'pady': 0, 'side': 'right'}
import tkinter as tk
from tkinter import *
my_w=tk.Tk()
l1 = tk.Listbox(my_w,height=3,bg='yellow')
l1.pack(side=LEFT) #
b1=tk.Button(my_w,text='RIGHT')
b1.pack(side=RIGHT) # Placed right of Listbox
my_w.pack_propagate(0)
my_w.mainloop()
print(my_w.slaves())
Output
[<tkinter.Listbox object .!listbox>, <tkinter.Button object .!button>]
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 0ipady : internal vertical padding , default is 0padx : External horizontal padding , default is 0pady : External vertical padding , default is 0side : which side the widget is to be packed 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()
The output of print(b1.pack_info()) is here .
{'in': <tkinter.Tk object .>, 'anchor': 'center', 'expand': 1,
'fill': 'y', 'ipadx': 10, 'ipady': 10, 'padx': 10, 'pady': 10, 'side': 'right'}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.