How to get grid information from pressed Button in tkinter?

Organizing widgets in a grid layout is an important aspect of GUI design using Tkinter. In this tutorial, we will explain how you can obtain the row and column indices of widgets within a grid layout when they are clicked.

What is Tkinter Grid Layout?

Tkinter's grid geometry manager is widely used for arranging widgets in rows and columns. When widgets are placed using the grid() method, they are assigned specific row and column indices. To understand how to retrieve this information dynamically, we'll first delve into the basics of grid layout in Tkinter.

Example

import tkinter as tk

# Create the main Tkinter window
root = tk.Tk()
root.title("Tkinter Grid Information")
root.geometry("720x250")

# Create widgets and position them using the grid method
label1 = tk.Label(root, text="Label 1")
label1.grid(row=0, column=0)

label2 = tk.Label(root, text="Label 2")
label2.grid(row=0, column=1)

button = tk.Button(root, text="Press Me")
button.grid(row=1, column=0)

# Run the Tkinter event loop
root.mainloop()

In the example above, we've created a simple Tkinter window with two labels and a button, positioned using the grid() method. The labels are in the first row, and the button is in the second row.

Retrieving Grid Information from Pressed Buttons

To dynamically retrieve the grid information of a widget when it is pressed, we can use the bind() method to associate a callback function with a button click event. The key is using the grid_info() method.

Example

import tkinter as tk

def button_pressed(event):
    # Get the widget that triggered the event
    widget = event.widget
    
    # Get the grid information of the widget
    grid_info = widget.grid_info()
    
    # Print the grid information
    print("Row:", grid_info['row'])
    print("Column:", grid_info['column'])
    print("All grid info:", grid_info)

# Create the main Tkinter window
root = tk.Tk()
root.title("Grid Information from Pressed Button")
root.geometry("400x200")

# Create multiple buttons in different positions
for row in range(2):
    for col in range(3):
        button = tk.Button(root, text=f"Button {row},{col}")
        button.grid(row=row, column=col, padx=5, pady=5)
        # Bind the left mouse button click event
        button.bind('<Button-1>', button_pressed)

# Run the Tkinter event loop
root.mainloop()

In this example, we've added the button_pressed() function, which takes an event as an argument. Inside the function, we retrieve the widget that triggered the event using event.widget. We then use the grid_info() method to obtain complete grid information of the widget, including the row and column indices.

Understanding grid_info() Return Values

The grid_info() method returns a dictionary containing all grid configuration options for the widget ?

import tkinter as tk

def show_grid_details(event):
    widget = event.widget
    info = widget.grid_info()
    
    print(f"Widget text: {widget['text']}")
    print(f"Row: {info['row']}")
    print(f"Column: {info['column']}")
    print(f"Sticky: {info['sticky']}")
    print(f"Padx: {info['padx']}")
    print(f"Pady: {info['pady']}")
    print("-" * 30)

root = tk.Tk()
root.title("Detailed Grid Information")

# Create button with specific grid options
button1 = tk.Button(root, text="Button with Padding")
button1.grid(row=0, column=0, sticky="ew", padx=10, pady=5)
button1.bind('<Button-1>', show_grid_details)

button2 = tk.Button(root, text="Center Button")  
button2.grid(row=1, column=1, sticky="nsew")
button2.bind('<Button-1>', show_grid_details)

root.mainloop()

Practical Use Cases

Understanding how to retrieve grid information from pressed buttons opens up various possibilities ?

  • Board Games Implement chess, tic-tac-toe, or checkers by detecting which grid cell was clicked.

  • Data Tables Create interactive spreadsheet-like interfaces where clicking cells triggers specific actions.

  • Dynamic Layouts Modify widget properties or layout based on their grid position.

  • Form Navigation Navigate between form fields based on their grid coordinates.

Comparison

Method Purpose Returns
grid_info() Get all grid configuration Dictionary with all options
event.widget Get the clicked widget Widget object
bind() Attach event handler None

Conclusion

Use grid_info() with event binding to retrieve grid positions of clicked widgets. This technique is essential for creating interactive grid-based applications like games and data tables in Tkinter.

Updated on: 2026-03-27T16:24:56+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements