Python Tkinter Geometry Manager
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.
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.
1. Tkinter pack() Geometry Manager
Packing Algorithm:
The steps of Packing algorithm are as follows:
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
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.pack()
frame2.pack()
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.
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.pack(fill=tk.X)
frame2.pack(fill=tk.X)
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()
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.
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
ipadx, ipady
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.
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.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
label.pack()
win.mainloop()
Copy
3. Trinket place() Geometry Manager
Copy
The possible options as a parameter to this method are given below:
x, y
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
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.
top = Tk()
top.geometry("400x250")
Username = Label(top, text = "Username").place(x = 30,y = 50)
top.mainloop()
Copy