jonjon1200
jonjon1200

Reputation: 11

Find an item by its position in a grid tkinter

In tkinter, is there a way for me to reference a widget within a grid by its row and column, in the same way that you would be able to reference an item within a list (or list of lists) by knowing its position in the list?

Upvotes: 1

Views: 3039

Answers (2)

jonjon1200
jonjon1200

Reputation: 11

Actually, I realised that I could solve my own problem in a much simpler way, by literally making a list of lists, with each sub-list containing all of the widgets for a single row, and therefore I can refer to each item through it's row and column.

Upvotes: 0

jasonharper
jasonharper

Reputation: 9622

You can call the .grid_slaves(row, column) method on the parent widget; this will return a list (possibly empty) of the widgets in that cell.

You could also iterate over all of the child widgets (.grid_slaves() with no parameters, or .winfo_children()) and call .grid_info() on each one. This returns a dictionary with 'row' and 'column' keys, along with various other grid parameters.

Upvotes: 7

Related Questions