How to set a certain number of rows and columns of a Tkinter grid?

In Tkinter, you can set the GUI of the application by using different geometry managers. The grid geometry manager is one of the most useful geometry managers in Tkinter that arranges widgets in a 2D table-like structure.

With the grid geometry manager, you can set a certain number of rows and columns and place widgets at specific locations. To control the grid size, you'll use rowconfigure() and columnconfigure() methods to define the dimensions and behavior of your grid layout.

Basic Grid Layout Example

Here's how to create labels and position them using the grid geometry manager ?

# Import the required library
from tkinter import *

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")
win.title("Grid Layout Example")

# Add label widgets
label1 = Label(win, text='Label1', font=("Calibri", 15), bg="lightblue")
label1.grid(column=1, row=2, padx=10, pady=10)

label2 = Label(win, text='Label2', font=("Calibri", 15), bg="lightgreen")
label2.grid(column=3, row=5, padx=10, pady=10)

label3 = Label(win, text='Label3', font=("Calibri", 15), bg="lightyellow")
label3.grid(column=5, row=8, padx=10, pady=10)

label4 = Label(win, text='Label4', font=("Calibri", 15), bg="lightcoral")
label4.grid(column=7, row=11, padx=10, pady=10)

# Configure grid size - set minimum rows and columns
win.rowconfigure(12, minsize=30)    # Ensure at least 12 rows
win.columnconfigure(8, minsize=50)  # Ensure at least 8 columns

win.mainloop()

Setting Specific Grid Dimensions

You can also configure specific rows and columns with weights to control resizing behavior ?

from tkinter import *

win = Tk()
win.geometry("600x400")
win.title("Grid Configuration Example")

# Configure grid weights for resizing
for i in range(5):
    win.rowconfigure(i, weight=1)    # Equal row weights
    win.columnconfigure(i, weight=1) # Equal column weights

# Create a 3x3 grid of buttons
buttons = []
for row in range(3):
    for col in range(3):
        btn = Button(win, text=f"Button {row+1},{col+1}")
        btn.grid(row=row, column=col, sticky="nsew", padx=2, pady=2)
        buttons.append(btn)

win.mainloop()

Grid Configuration Methods

Method Purpose Common Parameters
rowconfigure() Configure row properties weight, minsize, pad
columnconfigure() Configure column properties weight, minsize, pad
grid() Place widget in grid row, column, sticky, padx, pady

Key Parameters

Grid placement parameters:

  • row, column − Position in the grid
  • sticky − How widget expands ("n", "s", "e", "w", or combinations)
  • padx, pady − External padding around the widget

Grid configuration parameters:

  • weight − How much extra space the row/column gets when resizing
  • minsize − Minimum size in pixels

Conclusion

Use rowconfigure() and columnconfigure() to set grid dimensions and behavior. The weight parameter controls resizing, while sticky determines how widgets expand within their cells.

Updated on: 2026-03-26T18:46:36+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements