Python tkinter pack

Basics of Python Tkinter

Managing layout while using widgets by using Pack.

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=LEFTPlaced at LEFT of Next element
side=RIGHTPlaced at RIGHT of Next element
fill=BOTTOMPlaced at BOTTOM of next element

side=left

Tkinter 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 )

Tkinter side=RIGHT
# 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=RIGH

Tkinter 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

Tkinter 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=XExpand horizontally to fill the available space
fill=YExpand vertically to fill the available space
fill=BOTHExpand horizontally & vertically to fill the available space

fill=X

Tkinter 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

Tkinter 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()

pack_forget()

pack_forget() 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()
import tkinter as tk
my_w=Tk()

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) 
my_str = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str, width=20 )
l1.pack(side=RIGHT) 
my_str.set("Hi Welcome")
my_w.mainloop()

pack_info()

Get all information as a dictionary about the widget.
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'}

pack_propagate(flag)

If the flag is set then it will resize the manager to hold all the widgets. This should be called from master widget as it controls the manager
import tkinter as tk
my_w=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()

pack_slaves()

get a list of widgets
print(my_w.slaves())
Output
[<tkinter.Listbox object .!listbox>, <tkinter.Button object .!button>]

pack options

pack option values 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
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'}

Grid Layout in Tkinter Python Tkinter Frame


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer