Search Here

Layout Management (Geometry Managers) in Tkinter

 In this article, you will learn about layout management in Tkinter. Until now you have learned windows, frames, and widgets in Tkinter. Layout management in Tkinter means that position the widgets on Tkinter Window or Frame.

How to Position Widgets in Tkinter - with Grid, Place or Pack

Layout Managers in Tkinter

Whenever you create a GUI application in Tkinter using Python, you need to place widgets (Buttons, Labels, Frames, etc) or position them in a required position, And these layouts are controlled by "Geometry Managers". 

In your application each Frame and Window can use only one geometry manager, but you can use different geometry manager in different frames.

Tkinter provides the following "Geometry Managers", to make a layout more attractive and user-friendly.



Name Description
pack() To place widgets in a Frame or Window in specified order, pack() geometry manager will be used. Specified order means that pack() places each Frame below the previously defined Frame. pack() accepts some keyword arguments to handle widgets placement. For example "fill" parameter will fill the Frames in both "tk.X" (Horizontal) and "tk.Y" (Vertical) direction. If you want to fill the Frame in both direction than you can use "tk.BOTH" and set the expand keyword argument to "True", this will make your Frames responsive.
place() By using place(), you can position your widgets in a Frame or Window in X or Y direction. place() geometry manager takes two argument "X and Y", which specify x and y coordinates from top left corner of the widget or frame. Remember these coordinates are measured in pixels. The drawback of using place() geometry manager is that its not responsive.
grid() grid() is the mostly used geometry manager to making a perfect GUI. grid() geometry manager have all the powers fo pack(). grid() divides the widget or frame in rows and columns. Both row and column indices starts with 0. 0 means zeroth rows or column, 1 means first row or column and so on. You can place your widgets by specifying row and column number.

The .pack() geometry manager



pack() geometry manager

The .place() geometry manager



place() geometry manager

The .grid() geometry manager



grid() geometry manager

Post a Comment

0 Comments