How to use grid geometry manager to position widgets in a grid in Tkinter Python?

share link

by Abdul Rawoof A R dot icon Updated: Jul 11, 2023

technology logo
technology logo

Solution Kit Solution Kit  

In programming, Tkinter is a popular Python library. It is used for creating graphical user interfaces (GUIs). The Tkinter library provides various widgets and tools to build GUI applications. It is one the essential components is the Tkinter grid.  

 

The Tkinter grid is a geometry manager. It helps you organize and layout widgets within a Tkinter window or frame. It provides a grid-based layout system. It allows you to divide the available space into rows and columns. You can then place widgets at specific positions within this grid.  

 

Here's a general overview of how the Tkinter grid works:  

  • Create a Tkinter window or frame.  
  • Specify the grid structure by defining the number of rows and columns.  
  • Create widgets (such as buttons, labels, or entry fields) that you want to place in the grid.  
  • Use each widget's grid () method to specify its position within the grid. You provide the row and column indices where you want the widget placed. For example, widget.grid(row=0, column=1) places the widget in the first and second columns.  
  • You can specify more options with the grid () method. Defining how the widget should expand or align within its grid cell is sticky.  
  • Repeat steps 4 and 5 for each widget you want to add.  
  • Finally, start the Tkinter event loop to display the window. It handles user interactions.  

 

Various types of grids are available, ranging from simple 2D grids to more complex 3D grids. Here are some common types:  

2D grid:  

  • Cartesian Grid: Consists of rows and columns forming a rectangular grid.  
  • Square Grid: Similar to a Cartesian grid, but with equal row and column spacing.  
  • Hexagonal Grid: Uses hexagons as the grid cells instead of squares.  

3D grid:  

  • Cubic Grid: A 3D extension of the Cartesian grid, where each cell forms a cube.  
  • Cuboid Grid: Similar to the cubic grid, but with rectangular cells instead of cubes.  
  • Tetrahedral Grid: Uses tetrahedrons as the grid cells, forming a 3D triangular grid.  

Structured Grids:  

  • Staggered Grid: Different properties are defined at different locations in a staggered manner. It provides a more accurate representation of certain physical phenomena.  
  • Block-Structured Grid: Consists of many interconnected rectangular or curvilinear grids. It is used for complex geometry.  

Unstructured Grids:  

  • Arbitrary Lagrangian-Eulerian (ALE) Grid: Combines elements of structured and unstructured grids. It is used for fluid dynamics simulations.  
  • Delaunay Triangulation: A method for creating unstructured grids in 2D and 3D. It is where the cells are built by connecting points based on the Delaunay criterion.  
  • Voronoi Diagram: Another approach is by creating unstructured grids. Each cell represents a region around a seed point and is defined as the set of points closest to that seed.  

 

The grid layout manager in Python Tkinter provides a sticky option. It allows aligning the widgets to the LEFT, RIGHT, TOP, and BOTTOM directions. Now that we have this covered let's use the Grid Manager in our application. Tkinter grid provides some manager classes to manage widgets such as pack(), grid (), and place. Tkinter has three built-in layout managers. It uses geometric methods to position buttons in an application frame. It includes a pack, grid, and place. The method will return a list of all the grid options for a widget and their values.  

 

This method registers a widget with the grid geometry manager. If you don't do this, the widget will exist but not be visible on the screen. The grid manager locates widgets in a 2D grid using row and column relative coordinates. When creating this layout, using the pack manager is possible. But it takes many extra frame widgets and much work to make things look good. This is a geometry manager to manage the widgets in a table-like structure. Usually, a 2D table contains these widgets in the parent widget, which is split into rows and columns.  

 

Here is an example of how to use the grid geometry manager to position widgets in a grid in Tkinter Python: