I'm trying to create a column of label containing images in a for loop, I want them to have a specific command when I do a right click a left click on the image, I searched a solution and found this question. The answer of BrenBarn worked well only if i use a button. But the event Button-1, isn't returning any value that can differentiate one label from another.
WORKING :
active="red"
default_color="white"
def main(height=5,width=5):
for x in range(width):
for y in range(height):
btn = tkinter.Button(frame, bg=default_color)
btn.grid(column=x, row=y, sticky=N+S+E+W)
btn["command"] = lambda btn=btn: click(btn)
for x in range(width):
Grid.columnconfigure(frame, x, weight=1)
for y in range(height):
Grid.rowconfigure(frame, y, weight=1)
return frame
def click(button):
print(button)
if(button["bg"] == active):
button["bg"] = default_color
else:
button["bg"] = active
w= main(10,10)
NOT WORKING :
active="red"
default_color="white"
def main(height=5,width=5):
for x in range(width):
for y in range(height):
btn = tkinter.Button(frame, bg=default_color)
btn.grid(column=x, row=y, sticky=N+S+E+W)
btn.bind("<Button-1>", lambda btn=btn: click(btn))
for x in range(width):
Grid.columnconfigure(frame, x, weight=1)
for y in range(height):
Grid.rowconfigure(frame, y, weight=1)
return frame
def click(button):
print(button)
if(button["bg"] == active):
button["bg"] = default_color
else:
button["bg"] = active
w= main(10,10)