tkinter GUI list of buttons [closed]
I can't (easily) test this because the code in your question isn't complete enough to be runnable, but I think the problem is with the img
variable's value being changed by the for
loop.
Working around that might be a simple as passing the current value as a default argument to the lambda
function you define—similar to what you did for the x=emoji
argument to capture the current value of emoji
and have it passed it to the anonymous function being created:
for emoji, descriptor in emoji_descriptor img = ImageTk.PhotoImage(Image.open("emojis/"+descriptor+".png")) b = Button(emojiApp, text=descriptor, command=lambda x=emoji, img=img: appendEmoji(x), image=img) # CHANGED. b.pack()
If that doesn't work, another alternative would be to save the current PhotoImage
value by making it an attribute of the Button
after it's created:
for emoji, descriptor in emoji_descriptor img = ImageTk.PhotoImage(Image.open("emojis/"+descriptor+".png")) b = Button(emojiApp, text=descriptor, command=lambda x=emoji: appendEmoji(x), image=img) b.img = img # ADDED to save value of img b.pack()