Tkinter Pack Method in Python

Pack() Method




Tkinter also offers access to the geometric configuration of the widgets which can organize the widgets in the parent windows. There are mainly three geometry manager classes class.

Thete 3 Methids In Python Tkinter

Pack() method:-
 It organizes the widgets in blocks before placing in the parent widget.

Grid() method:-
It organizes the widgets in grid (table-like structure) before placing in the parent widget.

Place() method:-
It organizes the widgets by placing them on specific positions directed by the programmer.



Pack() Method:-

- Pack is the easiest to use of the three geometry managers of Tk and Tkinter. 
- Instead of having to declare precisely where a widget should appear on the display screen, we can declare the positions of widgets with the pack command relative to each other.  
- The pack command takes care of the details.  
- Though the pack command is easier to use, this layout managers is limited in its possibilities compared to the grid and place mangers.  
- For simple applications it is definitely the manager of choice. 
-  For example simple applications like placing a number of widgets side by side, or on top of each other.

Example:-

from Tkinter import *


root = Tk()



Label(root, text="Red Sun", bg="red", fg="white").pack()

Label(root, text="Green Grass", bg="green", fg="black").pack() 

Label(root, text="Blue Sky", bg="blue", fg="white").pack()



mainloop()

OutPut:-







Fill: If you want to make the widgets as wide as the parent widget, you have to use the fill=X option:
from Tkinter import *

root = Tk()
w = Label(root, text="Red Sun", bg="red", fg="white") 
w.pack(fill=X) 
w = Label(root, text="Green Grass", bg="green", fg="black") 
w.pack(fill=X)
w = Label(root, text="Blue Sky", bg="blue", fg="white") 
w.pack(fill=X) 



mainloop()



Padding:-
The pack() manager knows four padding options, i.e. internal and external padding and padding in x and y direction:

padx     External padding, horizontally 
pady     External padding, vertically 
ipadx    Internal padding, horizontally. 
ipady    Internal padding, vertically


How Horizontally Working in Tkinter:-


padx External padding, horizontally


from Tkinter import *



root = Tk() 



w = Label(root, text="Red Sun", bg="red", fg="white")

w.pack(fill=X,padx=10)

w = Label(root, text="Green Grass", bg="green", fg="black") 
w.pack(fill=X,padx=10) 
w = Label(root, text="Blue Sky", bg="blue", fg="white") 
w.pack(fill=X,padx=10) 

mainloop()


How Vertically Working in Tkinter:-


pady External padding, vertically

from Tkinter import *

root = Tk()

w = Label(root, text="Red Sun", bg="red", fg="white")
w.pack(fill=X,pady=10)
w = Label(root, text="Green Grass", bg="green", fg="black")
w.pack(fill=X,pady=10)
w = Label(root, text="Blue Sky", bg="blue", fg="white")
w.pack(fill=X,pady=10)

mainloop()

How Horizontally Working in Tkinter:-


ipadx Internal padding, horizontally.

from Tkinter import * 

root = Tk()

w = Label(root, text="Red Sun", bg="red", fg="white") 
w.pack() 
w = Label(root, text="Green Grass", bg="green", fg="black")
w.pack(ipadx=10) 
w = Label(root, text="Blue Sky", bg="blue", fg="white") 
w.pack() 

mainloop()








How Vertically Working in Tkinter:-

ipady Internal padding, vertically 


from Tkinter import * 

root = Tk() 

w = Label(root, text="Red Sun", bg="red", fg="white") 
w.pack() 
w = Label(root, text="Green Grass", bg="green", fg="black") 
w.pack(ipadx=10)
 w = Label(root, text="Blue Sky", bg="blue", fg="white") 
w.pack(ipady=10) 

mainloop()

The default value in all cases is 0. 


Placing widgets side by side


from Tkinter import *

root = Tk()

w = Label(root, text="red", bg="red", fg="white")
w.pack(padx=5, pady=10, side=LEFT)
w = Label(root, text="green", bg="green", fg="black")
w.pack(padx=5, pady=20, side=LEFT)
w = Label(root, text="blue", bg="blue", fg="white")
w.pack(padx=5, pady=20, side=LEFT)

mainloop()


To Next Artical We Discuss About How to Use grid And Place Method In Python...





Basic introduction To Tkinter and how to install Click The Link




Check Out Our Python ALL Post In Just One Click....





Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)

Post a Comment

8 Comments