I’ve got a little piece of code here and I can’t for the life of me work out why it’s not working as I expect. I know there are other ways to achieve this so (respectfully) I’m not looking for alternatives.
def setColour(c):
global colour
colour = c
print(colour)
for i, j in ((1,'red'),(2,'green'),(3,'blue'),(4,'yellow'),(5,'black')):
canvas.tag_bind(i, '<Button-1>', lambda _: setColour(j))
It works in a fashion; each canvas item for which the event is bound to the handler does invoke setColour() when clicked, however the value of colour is always set to 'black', as revealed by the print() statement.