I am trying to remove items from an array by clicking a button in Python with Tkinter but currently, all the remove buttons just remove the last item. How do I get each of the remove buttons to remember exactly which item in the array they were supposed to remove? Here is a simplified version of my code:
#Setup Tkinter
from tkinter import *
root = Tk()
# Array Foods
foods = ["apple", "pizza", "tomato", "chips"]
def removeApp(food):
# Resets entire list
for widget in root.winfo_children():
widget.destroy()
# Removes one food and relist everything
foods.pop(food)
listFoods()
def listFoods():
# Loop though and print food and a remove button for each food
for i in range(len(foods)):
Label(root, text=foods[i]).pack()
Button(root, text="remove", command=lambda: removeApp(i)).pack()
listFoods()
root.mainloop()