EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Tkinter Pack

Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter Pack

Tkinter Pack

Introduction to Tkinter Pack

To design any GUI applications in Python, we have a Tkinter package. So in this GUI application, there are different widgets to be placed or organize. This organization of widgets is done by layout managers or geometry managers. In Python, Tkinter provides three different geometry managers like grid, pack, and place managers. In this article, we are going to discuss the pack manager or method in Tkinter for organizing widgets. The pack manager puts the widgets in horizontal and vertical boxes or blocks in the parent window or widget. This manager is used to handle the widgets when the widgets need to be inside the frames or to place widgets on top of each other or widgets side by side.

Working of Pack Manager with Examples

Tkinter provides a geometry manager pack which is limited in use than the grid manager, but the pack manager is to organize the widgets in boxes or blocks before placing these widgets in the root or master window or widget. We have to note that before using this manager it is compulsory to import the Tkinter package.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

widget.pack(options)

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

Options: Pack managers have a few options they are:

1. Fill

This option is used when the widgets are needed to fill the same as the size of the parent widget or by default it takes some minimum space to fill. It also has attributes like “none” which default, X to fill horizontally, Y to fill vertically, or X and Y both horizontally and vertically.

Example:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
box_1 = tk.Label(root, text="Horizontally filled", bg="green")
box_1.pack(fill="x")
box_2 = tk.Label(root, text="Vertically filled", bg="blue")
box_2.pack(fill="y")
box_3 = tk.Label(root, text="Using both Horizontally and Vertically", bg="red")
box_3.pack(fill= "both")
root.mainloop()

Output:

Tkinter Pack-1.1

In the above code, we are demonstrating the use of fill option with all the attributes, to fill the box horizontally it is declared as “x” and is green in color. To fill vertically it has been declared as “y” and is blue in color consumes all vertical space in that row, and lastly, the fill option is declared as “both” which is red in color so we can see the block growing horizontally. The output can be seen in how it will be displayed.

2. Expand

This is used for allocating space to the given widget with two attributes YES or No and TRUE or FALSE. If the attribute is YES or TRUE then the widgets expand to fill any space except the space used by the parent widget. In the above option in fill is used to determine how much of the space allocated a widget consumes, whereas expand allocates more space to a given widget only

Example:
import tkinter as tk
root = tk.Tk()
root.geometry("500x500")
box_1 = tk.Label(root, text="expand is true", bg="green")
box_1.pack(expand=True)
box_2 = tk.Label(root, text="expand is False", bg="red")
box_2.pack(fill="x",expand=False)
root.mainloop()

Output:

Tkinter Pack-1.2

In the above code, this is to demonstrate an expand option with the attributes True or False. In the code, it can be seen that the box with green in color is used to expand and is set to true, therefore, it is expanded only how much the widget is, and if expand option is false, then it will be filled horizontally as fill option attribute is “x” and is not expanded.

3. Side

This option is used when we want to place the widgets on which side of the parent widget. It has attributes like TOP or BOTTOM, LEFT or RIGHT. By default, this option takes the TOP attribute.

Example:
import tkinter as tk
root = tk.Tk()
root.geometry("500x500")
box_1 = tk.Label(root, text="placed at left", bg="green")
box_1.pack(side="left", expand=True, fill="both")
box_2 = tk.Label(root, text="placed at right", bg="red")
box_2.pack(side="right", expand=True, fill="both")
box_3 = tk.Label(root, text="placed on top", bg="blue")
box_3.pack(side="top", expand=True, fill="both")
box_4 = tk.Label(root, text="Placed at bottom", bg="yellow")
box_4.pack(side="bottom", expand=True, fill="both")
root.mainloop()

Output:

Tkinter Pack-1.3

The above example, it demonstrates the side option along with its attributes LEFT, RIGHT, TOP, and BOTTOM. So in the above code, you can see the green box is placed or packed on the left-hand side of the parent widget, the red box is placed on the right side, the blue box is placed on the top, and the yellow box is placed at the bottom of the parent widget as we can see in the output above.

In most cases users while designing GUI applications use a grid geometry manager than a pack manager, because the grid puts the widgets within the grid whereas the pack lays widgets out along the sides of the blocks or boxes. To note pack is mostly used when there are some very complex layouts are need to be created but it is really difficult to understand it completely so many of the users prefer the grid manager. One important thing is that we cannot create an app using both grids and pack within one parent no matter the app will work but it gets into an infinite loop as each manager tries to lay the widgets like one will change or adjust the size and other manger notices it and resize it so this creates confusion. Hence grid geometry manager is more flexible than the package manager.

Conclusion

In this article, we saw how to create a GUI application using the Tkinter package in Python. Tkinter provides three different geometry managers like a grid, pack, and place. In this, we have discussed the pack() manager. Pack() geometry manager is used to allocating or organize the widgets in blocks either horizontally or vertically. This manager provides three different options that are filled, expanded, and side by side. All these options are explained with examples and their attributes in the above article.

Recommended Articles

This is a guide to Tkinter Pack. Here we discuss the introduction and working of pack manager along with different examples and its code implementation. You may also look at the following articles to learn more –

  1. Tkinter Checkbutton
  2. Tkinter Spinbox
  3. Tkinter LabelFrame
  4. Tkinter Frame
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More