Python | pack() method in Tkinter

The Pack geometry manager packs widgets in rows or columns. We can use options like fill, expand, and side to control this geometry manager.

Compared to the grid manager, the pack manager is somewhat limited, but it’s much easier to use in a few, but quite common situations:

  • Put a widget inside a frame (or any other container widget), and have it fill the entire frame
  • Place a number of widgets on top of each other
  • Place a number of widgets side by side

Code #1: Putting a widget inside frame and filling entire frame. We can do this with the help of expand and fill options.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing tkinter module
from tkinter import * from tkinter.ttk import *
  
# creating Tk window
master = Tk()
  
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
  
# button widgets which can also expand and fill
# in the parent widget entirely
b1 = Button(pane, text = "Click me !")
b1.pack(fill = BOTH, expand = True)
  
b2 = Button(pane, text = "Click me too")
b2.pack(fill = BOTH, expand = True)
  
mainloop()

chevron_right


Output:

Code #2: Placing widgets on top of each other and side by side. We can do this by side option.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *
  
# creating Tk window
master = Tk()
  
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
  
# button widgets which can also expand and fill
# in the parent widget entirely
b1 = Button(pane, text = "Click me !"
            background = "red", fg = "white")
b1.pack(side = TOP, expand = True, fill = BOTH)
  
b2 = Button(pane, text = "Click me too"
            background = "blue", fg = "white")
b2.pack(side = TOP, expand = True, fill = BOTH)
  
b3 = Button(pane, text = "I'm also button",
            background = "green", fg = "white")
b3.pack(side = TOP, expand = True, fill = BOTH)
  
mainloop()

chevron_right


Output:

Code #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *
  
# creating Tk window
master = Tk()
  
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
  
# button widgets which can also expand and fill
# in the parent widget entirely
b1 = Button(pane, text = "Click me !"
            background = "red", fg = "white")
b1.pack(side = LEFT, expand = True, fill = BOTH)
  
b2 = Button(pane, text = "Click me too",
            background = "blue", fg = "white")
b2.pack(side = LEFT, expand = True, fill = BOTH)
  
b3 = Button(pane, text = "I'm also button",
            background = "green", fg = "white")
b3.pack(side = LEFT, expand = True, fill = BOTH)
  
mainloop()

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.