How To Position Widgets in Tkinter
Tkinter is the most popular way to create Graphical User Interfaces (GUIs) in Python. For building GUIs, Tkinter provides developers with a number of standard widgets, including buttons, labels and text boxes.
Each of these widgets need to be positioned for user accessibility and widget focus, and then programmed with underlying application logic so they can work as intended in response to mouse clicks and other actions.
Positioning Widgets with Layout Managers
Tkinter has three built-in layout managers that use geometric methods to position widgets in an application frame:
- pack() organizes widgets in horizontal and vertical boxes that are limited to left, right, top, bottom positions. Each box is offset and relative to each other.
- place() places widgets in a two dimensional grid using x and y absolute coordinates.
- grid() locates widgets in a two dimensional grid using row and column absolute coordinates.
Important: pack(), place(), and grid() should not be combined in the same master window. Instead choose one and stick with it.
Watch the video, which introduces the pack, place and grid code snippets below
Positioning Widgets With the Pack Layout Manager
pack is the easiest layout manager to use with Tkinter. Instead of declaring the precise location of a widget, pack() declares the positioning 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() has four padding options:
- Internal padding
- External padding
- Padx, which pads along the x axis
- Pady, which pads along the y axis
For more information about pack() and its options, refer to: How To Position Buttons in Tkinter With Pack
Vertical Positioning with Pack
In this example, three labels (widgets that contain text or images) are positioned vertically in relation to each other, and are not padded.
import tkinter as tk root = tk.Tk() test = tk.Label(root, text="Red", bg="red", fg="white") test.pack(side=tk.BOTTOM) test = tk.Label(root, text="Green", bg="green", fg="white") test.pack(side=tk.BOTTOM) test = tk.Label(root, text="Purple", bg="purple", fg="white") test.pack(side=tk.BOTTOM) tk.mainloop()
Side-by-Side Positioning with Pack
In this example, three labels are positioned side by side in relation to each other, and are padded to separate them:
import tkinter as tk root = tk.Tk() test = tk.Label(root, text="red", bg="red", fg="white") test.pack(padx=5, pady=15, side=tk.LEFT) test = tk.Label(root, text="green", bg="green", fg="white") test.pack(padx=5, pady=20, side=tk.LEFT) test = tk.Label(root, text="purple", bg="purple", fg="white") test.pack(padx=5, pady=20, side=tk.LEFT) root.mainloop()
Positioning Widgets With Place Layout Manager
place() lets you position a widget either with absolute x,y coordinates, or relative to another widget.
In this example, three labels are positioned diagonally using x,y coordinates.
from tkinter import * root = Tk() root.geometry('250x200+250+200') Label(root, text="Position 1 : x=0, y=0", bg="#FFFF00", fg="white").place(x=5, y=0) Label(root, text="Position 2 : x=50, y=40", bg="#3300CC", fg="white").place(x=50, y=40) Label(root, text="Position 3 : x=75, y=80", bg="#FF0099", fg="white").place(x=75, y=80) root.mainloop()
Positioning Widgets With Grid Layout Manager
grid() positions widgets in a two dimensional grid of rows and columns similar to a spreadsheet.
In this example, four labels are positioned in a grid of rows and columns:
from tkinter import * root = Tk() Label(text="Position 1", width=10).grid(row=0, column=0) Label(text="Position 2", width=10).grid(row=0, column=1) Label(text="Position 3", width=10).grid(row=1, column=0) Label(text="Position 4", width=10).grid(row=1, column=1) root.mainloop()
Positioning Tkinter.Ttk Widgets
The tkinter.ttk (Tile extension integrated into Tk) module provides access to the Tk themed widget set included in Tk 8.5 and greater.
Six new widgets are added by Ttk:
All six are subclasses of Widget.
There are differences in how the Ttk theme is coded, including alignment and padding options for the six new widgets. However, positioning with the pack, place and grid layout managers remains the same as Tk.
To start using Ttk, enter the following import statement in a Python console:
from tkinter import ttk
To override Tk widgets, the ttk import must follow the tk import:
from tkinter import * from tkinter.ttk import *
Importing tkinter.ttk after importing tkinter causes Ttk widgets to automatically replace the standard Tk widgets of Button, Checkbutton, Entry, Frame, Label, LabelFrame, Menubutton, PanedWindow, Radiobutton, Scale and Scrollbar.
Important: The basic idea is to separate as much as possible the code implementing a widget’s behavior from the code implementing its appearance (Style()).
Keep in mind that while Ttk widgets have a Style()padding option, it does not affect widget positioning.
Positioning Ttk widgets with Pack
In this example, a ttk.button is positioned with pack() and is padded.
from tkinter import * from tkinter.ttk import * root = Tk() # Style() padding adds pixels inside the Button. The widget’s position is not changed. ttk.Style().configure("TButton", padding=5, relief="flat") button1 = ttk.Button(text="Button Example") # pack() padding adds pixels outside the TButton. The widget’s position is changed. button1.pack(side = BOTTOM, padx=<x_coordinate, pady=<y_coordinate>) root.mainloop()
Now that you know how to add widgets using Python’s Tkinter, 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)