How to access a button in a grid of buttons using a 2D list in tkinter? How to access a button in a grid of buttons using a 2D list in tkinter? tkinter tkinter

How to access a button in a grid of buttons using a 2D list in tkinter?


The problem is in the way you construct the list

btns=[[None]*5]*5

in this way you create a list and moltiplicate its reference per 5 times.As this each time that it loops for add a button in the row list the same changes affects the other row lists.

EX

btns = [[None]*5]*5btns[0][0] = 'a'btns ---> [['a', None, None, None, None],['a', None, None, None, None],['a', None, None, None, None],['a', None, None, None, None],['a', None, None, None, None]]

This is the correct way of build the list

btns = [[None for i in range(rows)] for j in range(columns)]