Python Tkinter Geometry Managers
In this article, you will learn different Python Tkinter Geometry managers. Geometry managers help to position widgets.
Tkinter is the standard GUI library that provides a fast and easy way to create GUI applications. It is also called the Tk interface. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. The greatest strength of Tkinter is its straightforwardness. It accompanies a wide scope of essential widgets for almost all common tasks, such as text, catches, radio catches, checkboxes, comboboxes, scales, names, or drawing canvas.
Tkinter contains three geometry managers-
- Pack()
- Place()
- Grid()
Tkinter Packer Manager
Tkinter pack() method packs widgets in blocks before placing them in the parent widget. It determines the size and alignment of all your widgets. The process of packer is similar to how we pack elements inside a suitcase when travelling.
Syntax of Pack Manager
widget.pack(option)
from tkinter import *
w = Tk()
w.geometry("200x100")
l1 = Label(w, text="January", bg="#FFB857")
l1.pack()
l2 = Label(w, text="February", bg="#8EC2C9")
l2.pack()
l3 = Label(w, text="March", bg="#FF7182")
l3.pack()
mainloop()
The option in pack() method can be fill, expand and side.
Fill
It controls how to fill the widget space. It fills the space according to the given attribute. It accepts the following attributes-
NONE (Default), X (Fill Horizontally), Y (Fill Vertically) and BOTH (Fill both Horizontally and Vertically)
from tkinter import *
w = Tk()
w.geometry("200x100")
l1 = Label(w, text="January", bg="#FFB857")
l1.pack(fill=X)
l2 = Label(w, text="February", bg="#8EC2C9")
l2.pack(fill=X)
l3 = Label(w, text="March", bg="#FF7182")
l3.pack(fill=X)
mainloop()
Expand
The 'Expand' accepts two attributes- YES or NO. It set to YES, the widget expands to fill space not used in parent widget.
from tkinter import *
w = Tk()
w.geometry("200x100")
l1 = Label(w, text="January", bg="#FFB857")
l1.pack(expand=YES)
l2 = Label(w, text="February", bg="#8EC2C9")
l2.pack(expand=NO)
l3 = Label(w, text="March", bg="#FF7182")
l3.pack(expand=YES)
mainloop()
Side
Determines which side of parent widget it packs to. It has the following attributes-
TOP(Default), BOTTOM, LEFT, RIGHT
from tkinter import *
w = Tk()
w.geometry("200x100")
l1 = Label(w, text="January", bg="#FFB857")
l1.pack(side=TOP)
l2 = Label(w, text="February", bg="#8EC2C9")
l2.pack(side=RIGHT)
l3 = Label(w, text="March", bg="#FF7182")
l3.pack(side=LEFT)
mainloop()
Placer Manager
Tkinter place() method provides the size of the widgets and locations to place them. It organizes all the widgets in a specific position in a parent widget
The options in place() method can be anchor, bordermode, height, width, relheight, relwidth, relx, rely, x and y.
OPTIONS |
DESCRIPTIONS |
Anchor | Indicates the compass directions, where the widget is anchored to, like - N, E, W, S, NE, ES, NW or SW. |
Bordermode | The Bordermode has two options Inside and Outside. The inside indicates that the other options refer to the parent's inside. |
Width, Height | It specifies the width and height of widget in pixels. |
Relwidth, Relheight | It specifies the width and height of the parent widget in fraction, i.e., between 0.0 to 1.0. |
Relx, Rely | It specifies horizontal and vertical offset as a fraction of the width of parent widget, i.e., between 0.0 to 1.0. |
X, Y | It specifies horizontal and vertical offset in pixels. |
These are the examples of place geometry manager.
Width, Height
from tkinter import *
root = Tk()
root.geometry("200x120")
btn_height = Button(root, text="40px high", bg="#FFB857")
btn_height.place(height=50, x=5, y=20)
btn_width = Button(root, text="80px wide", bg="#FF7182")
btn_width.place(width=80, x=30, y=80)
root.mainloop()
Relwidth, Relheight
from tkinter import *
root = Tk()
root.geometry("200x100")
btn_height = Button(root, text="0.2 relheight", bg="#FF7182")
btn_height.place(relheight=0.4, x= 5, y = 2)
btn_width = Button(root, text="0.6 relwidth", bg="#BEB9B5")
btn_width.place(relwidth=0.6, x = 20, y = 60)
root.mainloop()
Relx, Rely, Relx, Rely
from tkinter import *
root = Tk()
root.geometry("200x100")
btn_height = Button(root, text="0.2 relheight", bg="#FF7182")
btn_height.place(relheight=0.4, relx= 0.2, y = 2)
btn_width = Button(root, text="0.6 relwidth", bg="#BEB9B5")
btn_width.place(relwidth=0.6, x = 20, rely = 0.5)
root.mainloop()
Grid Manager
Tkinter grid() method organises widgets in a table like structure in the parent widget. It is most flexible geometry manager in Tkinter. It splits the parent widget in rows and columns and use the grid method to specify the row and column to place the widget. Syntax of grid manager -
widget.grid(options)
The options in grid() method can be column, columnspan, ipadx, ipady, padx, pady, row, rowspan and sticky.
OPTIONS |
DESCRIPTIONS |
column | Insert a widget in a column. The default column is 0, which is the leftmost column. |
columnspan | Number of column widget takes up. Default is 1. |
ipadx, ipady | The pixels to pad widget horizontally and vertically inside the widget's borders. |
padx, pady | The pixels to pad widget horizontally and vertically outside the widget's borders. |
row | The row to put widget in. The default is 0. |
rowspan | The number of rows the widget takes up. The default is 1. |
sticky | The sticky is used when the widget is smaller than the cell. It Indicates the compass directions, where the widget sticks to, like - N, E, W, S, NE, ES, NW or SW. |
These are the examples of grid geometry manager.
Column, Columnspan
from tkinter import *
root = Tk()
root.geometry("200x100")
b1 = Button(root, text="Column 3",bg="#D5FF48")
b1.grid(column=3)
b2 = Button(root, text="Columnspan 3",bg="#B3B1B1")
b2.grid(columnspan=3)
root.mainloop()
ipadx, ipady, padx, pady
from tkinter import *
root = Tk()
root.geometry("200x90")
b1 = Button(root, text="ipadx",bg="#07F7AD")
b1.grid(ipadx=6, ipady=4)
b2 = Button(root, text="ipady",bg="#0BDEF2")
b2.grid(ipady=4)
root.mainloop()
Row, Rowspan
from tkinter import *
root = Tk()
root.geometry("200x100")
b1 = Button(root, text="Row3", bg="#D5FF48")
b1.grid(row=3)
b2 = Button(root, text="Rowspan3", bg="#B3B1B1")
b2.grid(rowspan=3)
root.mainloop()
Sticky
from tkinter import *
root = Tk()
root.geometry("200x100")
b1 = Button(root, text="Stick to NE", bg="#07F7AD")
b1.grid(sticky=NE)
root.mainloop()
Related Articles
Python Tkinter Combobox Event BindingPython Tkinter Combobox
Add background image in Python Tkinter
Python Tkinter Text Widget
Countdown clock and timer using Tkinter in Python
Python Tkinter Frame Widget
Python Tkinter Checkbutton Widget
Entry Field Validation in Tkinter Python
Python Tkinter Tutorial with Examples
Python Tkinter Scale Widget
Python3 Tkinter Messagebox
Python 3 Tkinter Menu Bar
Python Tkinter Geometry Managers
Python get visitor information by IP address
Python OpenCV ColorMap
Scientific calculator in Python
Entry Field Validation in Tkinter Python
Python take screenshot of specific window