I'm writing a program that displays a grid of buttons, when a button is pressed I want it to print the location of the button in the grid ("row column") out to the console. Here is what I have
import Tkinter as tk
class board(tk.Tk):
def __init__(self, parent=None):
tk.Tk.__init__(self,parent)
self.rows = 5
self.columns = 5
self.init_board()
def init_board(self):
for i in range(self.rows):
for j in range(self.columns):
cmd = lambda: self.button_callback(i,j)
b = tk.Button(self, text=str(" "), command=cmd)
b.grid(row=i, column=j)
def button_callback(self, row, col):
print(str(row) + " " + str(col))
if __name__ == '__main__':
board().mainloop()
the problem is that when I click on any of the buttons I get "4 4" printed out which is the location of the last button instantiated in the loop. I don't know why this is happening, please help!