0

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()
Art
  • 2,524
  • 4
  • 13
  • 25
ctdgunner
  • 5
  • 4
  • yes, with some slight modification it did adding, `food=food` after my lambda in `listFoods()` it seems to remember whats what now. Thank you. – ctdgunner Nov 01 '21 at 03:59

0 Answers0