I know that this is quite an individual question, but I think it is a problem in Tkinter as I regularly encounter similar problem that I workaround and the answers could be beneficial for others as well.
The script is:
from tkinter import *
import random
class gui:
def __init__(self):
win=self.win=Tk()
win.title('Ploters Data!')
def identifier(self,x,y):
print('id',x,y)
def createGraph(self,rows,columns):
for xrow in range(rows+1):
for ycolumn in range(columns+1):
if xrow == 0 or ycolumn == 0:
text = '--'
if xrow == 0:
if ycolumn==5:
text='5'
if ycolumn==10:
text='10'
if ycolumn == 0:
if xrow==5:
text='5'
if xrow==10:
text='10'
if xrow == ycolumn == 0:
text='[]'
pixel = Button(self.win,padx=10,pady=10,text=text)
# print('click',xrow,ycolumn)
pixel.config(command=lambda button=pixel: self.identifier(xrow,ycolumn))
pixel.grid(row=xrow,column=ycolumn)
else:
pixel = Button(self.win,padx=10,pady=10)
# print('click',xrow,ycolumn)
pixel.config(command=lambda button=pixel: self.identifier(xrow,ycolumn))
pixel.grid(row=xrow,column=ycolumn)
# print(xrow,ycolumn)
self.win.mainloop()
s=gui()
s.createGraph(15,10)
The specific problem is that when you click a button on the grid it doesn't give the correct 'coordinates', instead, it gives the last button.