How To Use Pack In Tkinter
Tkinter is the most popular package for designing Python application GUIs. Tkinter provides a number of widgets you can use to design your GUI. Widgets can be added to a container or frame, and then programmed for use.
Tkinter has three Layout Managers that use geometric methods to position widgets in an application frame: pack, place, grid. Each method has different coding requirements. Note that if a widget is added to a frame without using a Layout Manager the code may not produce an error, but the widget will not be displayed in the frame.
Pack Layout Manager
pack is the easiest Layout Manager to code with in Tkinter.
Instead of declaring the precise location of a widget, the pack() method declares the position of widgets in relation to each other. However, pack() is limited in precision compared to place() and grid() which feature absolute positioning. For simple positioning of widgets vertically or horizontally in relation to each other, pack() is the Layout Manager of choice.
pack() organizes widgets in horizontal and vertical boxes that are limited to left, right, top, bottom positions offset and relative to each other within a frame.
pack() options described in this section are implemented in code in examples that follow.
- side is the most basic option, and includes padding options for positioning widgets in relation to the left, right, top, bottom sides of the widget, and relative to each other.
- fill packs a widget inside a container, filling the entire container.
- expand is an option for assigning additional space to the widget container.
pack() also has padding options:
- padx, which pads externally along the x axis.
- pady, which pads externally along the y axis.
- ipadx, which pads internally along the x axis.
- ipady, which pads internally along the y axis.
Tkinter Pack Button Example 1
In this example, a button is added with pack() without specifying which side. The button adjoins the top side of the frame by default:
from tkinter import * root = Tk() root.geometry('200x150') button1 = Button(text="Button1") button1.pack() root.mainloop()
Tkinter Pack Button Example 2
In this example, pack() positions four buttons to the left, right, top, bottom sides of a frame. Due to the geometry of the frame and the lack of padding, the position of the buttons may appear slightly uneven in relation to each other:
from tkinter import * root = Tk() root.geometry('250x150') button1 = Button(text="Left") button1.pack(side = LEFT) button2 = Button(text="Top") button2.pack(side = TOP) button3 = Button(text="Right") button3.pack(side = RIGHT) o button4 = Button(text="Bottom") button4.pack(side = BOTTOM) root.mainloop()
Tkinter Pack Button Example 3
In this example, two buttons are positioned in relation to the bottom side of a frame and from each other using padding:
from tkinter import * root = Tk() root.geometry('250x150') button1 = Button(text="button1") button1.pack(side = BOTTOM, pady=6) button2 = Button(text="button2") button2.pack(side = BOTTOM, pady=3) root.mainloop()
Tkinter Pack Fill Example
The pack() fill option is used to make a widget fill the entire frame. The pack() expand option is used to expand the widget if the user expands the frame.
- NONE (default), which will keep the widget’s original size.
- X, fill horizontally.
- Y, fill vertically.
- BOTH, fill horizontally and vertically.
- Numerical values.
In this example, a listbox fills the entire frame. If a user resizes the frame the listbox will expand with it:
from tkinter import * root = Tk() listbox = Listbox(root) # fill option added to make widget fill entire frame. # expand option added to expand widget, if user resizes frame. listbox.pack(fill=BOTH, expand=1) for z in range(<numerical_value>): listbox.insert(END, str(z)) mainloop()
Tkinter Pack() Padding Example
pack() has an ipadx option for internal padding to control widgets horizontally, and an ipady option to control widgets vertically. Only the internal width and height of widgets is affected, positioning is not changed.
In this example, pack() includes ipadx, ipady options to control the width and height of two text labels:
import tkinter as tk root = tk.Tk() test = tk.Label(root, text="Red", bg="red", fg="white") test.pack(ipadx=30, ipady=6) test = tk.Label(root, text="Purple", bg="purple", fg="white") test.pack(ipadx=8, ipady=12) tk.mainloop()
Now that you know how to use Tkinter’s Pack() layout manager, let’s move on to other things you can do with Tkinter:
Python For Data Science
Pre-bundled with the most important packages Data Scientists need, ActivePython is pre-compiled so you and your team don’t have to waste time configuring the open source distribution. You can focus on what’s important–spending more time building algorithms and predictive models against your big data sources, and less time on system configuration.
Some Popular Python Packages for Data Science/Big Data/Machine LearningYou Get Pre-compiled – with ActivePython
- pandas (data analysis)
- NumPy (multi-dimensional arrays)
- SciPy (algorithms to use with numpy)
- HDF5 (store & manipulate data)
- Matplotlib (data visualization)
- Jupyter (research collaboration)
- PyTables (managing HDF5 datasets)
- HDFS (C/C++ wrapper for Hadoop)
- pymongo (MongoDB driver)
- SQLAlchemy (Python SQL Toolkit)