Python Tkinter Geometry Manager
In this tutorial, we will learn how to control the layout of the Application with the
help of the Tkinter Geometry Managers.
Controlling Tkinter Application Layout
In order to organize or arrange or place all the widgets in the parent window,
Tkinter provides us the geometric configuration of the widgets. The GUI
Application Layout is mainly controlled by Geometric Managers of Tkinter.
It is important to note here that each window and Frame in your application is allowed
to use only one geometry manager. Also, different frames can use different
geometry managers, even if they're already assigned to a frame or window using
another geometry manager.
There are mainly three methods in Geometry Managers:
Let us discuss each method in detail one by one.
1. Tkinter pack() Geometry Manager
The pack() method mainly uses a packing algorithm in order to place widgets in
a Frame or window in a specified order.
This method is mainly used to organize the widgets in a block.
Packing Algorithm:
The steps of Packing algorithm are as follows:
Firstly this algorithm will compute a rectangular area known as
a Parcel which is tall (or wide) enough to hold the widget and then it will fill
the remaining width (or height) in the window with blank space.
It will center the widget until any different location is specified.
This method is powerful but it is difficult to visualize.
Here is the syntax for using pack() function:
widget.pack(options)
Copy
The possible options as a parameter to this method are given below:
fill
The default value of this option is set to NONE. Also, we can set it to X or Y in
order to determine whether the widget contains any extra space.
side
This option specifies which side to pack the widget against. If you want to
pack widgets vertically, use TOP which is the default value. If you want to
pack widgets horizontally, use LEFT.
expand
This option is used to specify whether the widgets should be expanded to
fill any extra space in the geometry master or not. Its default value is false. If it
is false then the widget is not expanded otherwise widget expands to fill extra
space.
Tkinter pack() Geometry Manager Example:
Let us discuss an example where we will see what happens when you pack() three
colored Frame widgets(here is the Tkinter Frame Widget) into a window:
import tkinter as tk
win = tk.Tk()
# add an orange frame
frame1 = tk.Frame(master=win, width=100, height=100,
bg="orange")
frame1.pack()
# add blue frame
frame2 = tk.Frame(master=win, width=50, height=50, bg="blue")
frame2.pack()
# add green frame
frame3 = tk.Frame(master=win, width=25, height=25, bg="green")
frame3.pack()
window.mainloop()
Copy
According to the output of the above code, the pack() method just places
each Frame below the previous one by default, in the same order in which they're
assigned to the window.
Tkinter pack() with Parameters
Let's take a few more code examples using the parameters of this function
like fill, side and, expand.
You can set the fill argument in order to specify in which direction you want the
frames should fill. If you want to fill in the horizontal direction then the option is tk.X,
whereas, tk.Y is used to fill vertically, and to fill in both directions tk.BOTH is used.
Let's take another example where we will stack the three frames so that each one fills
the whole window horizontally:
import tkinter as tk
win= tk.Tk()
frame1 = tk.Frame(master=win, height=80, bg="red")
# adding the fill argument with
# horizontal fill value
frame1.pack(fill=tk.X)
frame2 = tk.Frame(master=win, height=50, bg="yellow")
frame2.pack(fill=tk.X)
frame3 = tk.Frame(master=win, height=40, bg="blue")
frame3.pack(fill=tk.X)
win.mainloop()
Copy
In the above output, you can see that the frames fill the entire width of the
application window because we used the tk.X value for the fill parameter.
Now let's take another code example, where we will be using all options,
namely, fill, side, and expand options of the pack() method:
import tkinter as tk
win = tk.Tk()
frame1 = tk.Frame(master=win, width=200, height=100,
bg="Yellow")
# setting fill, side and expand
frame1.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
frame2 = tk.Frame(master=win, width=100, bg="blue")
frame2.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
frame3 = tk.Frame(master=win, width=50, bg="green")
frame3.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
win.mainloop()
Copy
If you will run this above code in your system then you can see this output is able to
expand in both directions.
2. Tkinter grid() Geometry Manager
The most used geometry manager is grid() because it provides all the power
of pack() function but in an easier and maintainable way.
The grid() geometry manager is mainly used to split either a window or frame into
rows and columns.
You can easily specify the location of a widget just by calling grid() function
and passing the row and column indices to the row and column keyword
arguments, respectively.
Index of both the row and column starts from 0, so a row index of 2 and a
column index of 2 tells the grid() function to place a widget in the third
column of the third row(0 is first, 1 is second and 2 means third).
Here is the syntax of the grid() function:
widget.grid(options)
Copy
The possible options as a parameter to this method are given below:
Column
This option specifies the column number in which the widget is to be placed.
The index of leftmost column is 0.
Row
This option specifies the row number in which the widget is to be placed.
The topmost row is represented by 0.
Columnspan
This option specifies the width of the widget. It mainly represents the number
of columns up to which, the column is expanded.
Rowspan
This option specifies the height of the widget. It mainly represents the number
of rows up to which, the row is expanded.
padx, pady
This option mainly represents the number of pixels of padding to be added
to the widget just outside the widget's border.
ipadx, ipady
This option is mainly used to represents the number of pixels of padding to be
added to the widget inside the widget's border.
Sticky
If any cell is larger than a widget, then sticky is mainly used to specify the
position of the widget inside the cell. It is basically concatenation of the
sticky letters which represents the position of the widget. It may be N, E, W, S,
NE, NW, NS, EW, ES.
Tkinter grid() Geometry Manager Example:
The following code script will help you to create a 5 × 3 grid of frames
with Label widgets packed into them:
import tkinter as tk
win = tk.Tk()
for i in range(5):
for j in range(3):
frame = tk.Frame(
master = win,
relief = tk.RAISED,
borderwidth = 1
frame.grid(row=i, column=j)
label = tk.Label(master=frame, text=f"Row {i}\nColumn
{j}")
label.pack()
win.mainloop()
Copy
If you want to add some padding then you can do it by using the following code
snippet:
import tkinter as tk
win = tk.Tk()
for i in range(5):
for j in range(3):
frame = tk.Frame(
master=win,
relief=tk.RAISED,
borderwidth=1
frame.grid(row=i, column=j, padx=5, pady=5)
label = tk.Label(master=frame, text=f"Row {i}\nColumn
{j}")
label.pack()
win.mainloop()
Copy
As you can see in the code example above, we have used
the padx and pady parameters because of which padding is applied outside the
widget. To add padding inside the Frame widget, use the
parameters ipadx and ipady in your code.
Similarly, do try using other parameters too for the grid() geometry manager.
3. Trinket place() Geometry Manager
The place() Geometry Manager organizes the widgets to place them in a specific
position as directed by the programmer.
This method basically organizes the widget in accordance with its x and y
coordinates. Both x and y coordinates are in pixels.
Thus the origin (where x and y are both 0) is the top-left corner of
the Frame or the window.
Thus, the y argument specifies the number of pixels of space from the top
of the window, to place the widget, and the x argument specifies the number
of pixels from the left of the window.
Here is the syntax of the place() method:
widget.place(options)
Copy
The possible options as a parameter to this method are given below:
x, y
This option indicates the horizontal and vertical offset in the pixels.
height, width
This option indicates the height and weight of the widget in the pixels.
Anchor
This option mainly represents the exact position of the widget within the
container. The default value (direction) is NW that is (the upper left corner).
bordermode
This option indicates the default value of the border type which
is INSIDE and it also refers to ignore the parent's inside the border. The other
option is OUTSIDE.
relx, rely
This option is used to represent the float between 0.0 and 1.0 and it is
the offset in the horizontal and vertical direction.
relheight, relwidth
This option is used to represent the float value between 0.0 and 1.0 indicating
the fraction of the parent's height and width.
Tkinter place() Geometry Manager Example:
The code snippet for this is given below:
from tkinter import *
top = Tk()
top.geometry("400x250")
Username = Label(top, text = "Username").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y =
130)
e1 = Entry(top).place(x = 80, y = 50)
e2 = Entry(top).place(x = 80, y = 90)
e3 = Entry(top).place(x = 95, y = 130)
top.mainloop()
Copy