title: Destroy an istance in python
body: <p>So, the problem is: instead of destroying an object when it exits the canvas, it destroys it when I close the window, can you tell me the error please?</p>
  <p>the objects are removed from the list, but they are not deleted, but if I delete the object before removing it from the list, it is not removed because it loses the assignment.</p>
  <pre><code>import time
  from tkinter import *
  from tkinter import ttk
  from random import randint
  
  root = Tk()
  root.title(&quot;Asteroid&quot;)
  root.geometry(&quot;+500+200&quot;)
  root.columnconfigure(0, weight=1)
  
  import time
  from tkinter import *
  from tkinter import ttk
  from random import randint
  
  root = Tk()
  root.title(&quot;Asteroid&quot;)
  root.geometry(&quot;+500+200&quot;)
  root.columnconfigure(0, weight=1)
  
  # Variabili
  
  end = False
  score = 0
  scoreVar = StringVar()
  scoreVar.set(str(score))
  list = []
  
  
  # Grafica
  
  scoreFrame = ttk.Frame(root)
  scoreFrame.grid(row=0, column=0, pady=10)
  
  text = ttk.Label(scoreFrame, text=&quot;Score:&quot;, font=(&quot;&quot;, 16))
  text.grid(row=0, column=0, padx=(0, 20))
  
  scoreText = ttk.Label(scoreFrame, textvariable=scoreVar, font=(&quot;&quot;, 16))
  scoreText.grid(row=0, column=1)
  
  
  canvas = Canvas(root, width=300, height=300, background=&quot;#0d1a51&quot;)
  canvas.grid(row=1, column=0)
  
  player = canvas.create_polygon(150, 150, 160, 180, 140, 180, 150, 150, outline=&quot;white&quot;, fill=&quot;#0d1a51&quot;)
  
  
  # Funzioni e classi
  
  def movePlayer(event, direction):
      if not end:
          coord = canvas.coords(player)
          if direction == &quot;up&quot; and coord[1] &gt; 8:
              canvas.move(player, 0, -8)
          elif direction == &quot;right&quot; and coord[2] &lt; 296:
              canvas.move(player, 8, 0)
          elif direction == &quot;left&quot; and coord[4] &gt; 4:
              canvas.move(player, -8, 0)
          elif direction == &quot;down&quot; and coord[3] &lt; 292:
              canvas.move(player, 0, 8)
  
  
  class Meteor:
      id = 0 #TODO: rimuovi
  
      def __init__(self):
          Meteor.id += 1 #TODO: rimuovi
          self.passed = False
          self.size = randint(25, 50)
          self.xCoord = randint(0 + self.size, 300 - self.size)
          self.spawnObject()
          self.collision()
          self.scoreUpdate(self.meteor)
  
      def getObject(self):
          return self.meteor
  
      def spawnObject(self):
          self.size = randint(25, 50)
          self.xCoord = randint(0 + self.size, 300 - self.size)
          self.meteor = canvas.create_oval(self.xCoord, 0, self.xCoord + self.size, self.size, outline=&quot;white&quot;)
          self.moveObject(self.meteor)
  
      def moveObject(self, meteor):
          canvas.move(meteor, 0, 5)
          canvas.after(50, lambda: self.moveObject(meteor))
  
      def collision(self):
          global end
          coord = canvas.coords(player)
          if not end:
              if canvas.find_overlapping(coord[0], coord[1], coord[2], coord[3]) != (1, ) or canvas.find_overlapping(coord[2], coord[3], coord[4], coord[5]) != (1,) or canvas.find_overlapping(coord[4], coord[5], coord[0], coord[1]) != (1, ):
                  end = True
                  canvas.delete(&quot;all&quot;)
                  endRound()
          canvas.after(1, self.collision)
  
      def scoreUpdate(self, meteor):
          global score
          if not end:
              if canvas.coords(meteor)[1] &gt;= 300 and not self.passed:
                  score += 1
                  scoreVar.set(str(score))
                  self.passed = True
              canvas.after(200, lambda: self.scoreUpdate(self.meteor))
  
      def __del__(self):
          print(&quot;Rimosso&quot;) #TODO: rimuovi
  
  
  def endRound():
      window = Toplevel()
      window.title(&quot;Game over&quot;)
      window.geometry(&quot;+550+300&quot;)
      window.focus()
  
      message = ttk.Label(window, text=&quot;&quot;)
  
  def spawn():
      if not end:
          temp = Meteor()
          list.append(temp)
          root.after(800, spawn)
      else:
          for object in list:
              del object
  
  def remove():
      i = 0
      if not end:
          while i &lt; len(list):
              object = list[i]
              if object.passed:
                  list.remove(object) # NON FUNZIONA (distrugge gli oggetti solo alla fine)
                  del object
                  i = 0
                  print(list) #TODO: rimuovi
                  print(Meteor.id) #TODO: rimuovi
              else:
                  i += 1
  
      root.after(50, remove)
  
  def reset():
      player = canvas.create_polygon(150, 150, 160, 180, 140, 180, 150, 150, outline=&quot;white&quot;, fill=&quot;#0d1a51&quot;)
      end = False
  
  
  spawn()
  remove()
  
  
  root.bind(&quot;&lt;Up&gt;&quot;, lambda event: movePlayer(event, &quot;up&quot;))
  root.bind(&quot;&lt;Right&gt;&quot;, lambda event: movePlayer(event, &quot;right&quot;))
  root.bind(&quot;&lt;Left&gt;&quot;, lambda event: movePlayer(event, &quot;left&quot;))
  root.bind(&quot;&lt;Down&gt;&quot;, lambda event: movePlayer(event, &quot;down&quot;))
  
  
  root.mainloop()
  </code></pre>
  <p>The object must be destroyed when his &quot;passed&quot; attribute is True.</p>