title: how to delete objects in tkinter?
body: <p>I want to program a program that will draw the text where I click and when I go through it it will be deleted. This is my code when I cross the line, everything is cleared and the ones I didn't cross are drawn back. Thanks to anyone who help me.</p>
  <pre class="lang-py prettyprint-override"><code>from tkinter import *
  
  class Program(Tk):
  
      def __init__(self):
          super().__init__()
          self.w, self.h = 250, 200
          self.canvas = Canvas(width=500, height=400)
          self.canvas.pack()
          self.draw_line()
          
          #self.x_lst_txt = None
          self.txt_lst_org = []
          self.txt2_lst_org = []
          self.change = False
          self.positon = False
          self.idx= 0
          self.new_lst=[]
  
          self.text1 = &quot;Programovanie&quot;
          self.text2 = &quot;Je zábava&quot;
          self.color1 = &quot;green&quot;
          self.color2 = &quot;orange&quot;
          self.opr1 = &quot;-&quot;
          self.opr2 = &quot;+&quot;
  
          self.all_binds()
  
      def draw_line(self):
          self.line = self.canvas.create_line(self.w, 0, self.w, self.h*2, width=5)
  
      def all_binds(self):
          self.canvas.bind(&quot;&lt;Button-1&gt;&quot;, self.draw_texts)
          self.canvas.bind(&quot;&lt;Button-3&gt;&quot;, self.draw_reacts)
          self.canvas.bind_all(&quot;&lt;Right&gt;&quot;, self.move_line_right)
          self.canvas.bind_all(&quot;&lt;Left&gt;&quot;, self.move_line_left)
  
      def draw_texts(self, event):
          self.xt = event.x
          self.yt = event.y
          uhol=self.yt
  
          if self.xt &lt; self.w:
              self.text_left = self.canvas.create_text(self.xt, self.yt, text=self.text1, fill=self.color1, angle=uhol)
              self.txt2_lst_org.append([self.xt, self.yt])
              self.change = True
          else:
              self.text_right = self.canvas.create_text(self.xt, self.yt, text=self.text2, fill=self.color2, angle=-uhol)
              self.txt_lst_org.append([self.xt, self.yt])
              self.change = True
  
      def draw_reacts(self, event):
          self.xr = event.x
          self.yr = event.y
          
          for i in range(10):
              rect = self.canvas.create_rectangle(self.xr, self.yr+i*10, self.xr+10, self.yr+10+i*10)
              self.yr += 5
  
      def select_x(self, lst):
          if self.change:
              for i in range(0, len(lst)):
                  if [len(i) for i in lst][self.idx] &gt; 1:
                      self.new_lst.append(lst[i][0])
                      self.idx+=1
              self.idx = 0
              self.change=False
              return sorted(self.new_lst)
          return sorted(self.new_lst)
  
      def delete_all(self, lst, x_lst):
          self.new_lst = sorted(self.new_lst, reverse=self.positon)
          x_lst = sorted(x_lst, reverse=self.positon)
          del lst[0]
          del x_lst[0]
          del self.new_lst[0]
  
      def replace_text(self, lst, txt, fill, opr):
          for i in range(0, len(lst)+1):
              x, y = lst[i]
              angle = y
              texts = self.canvas.create_text(x, y, text=txt, fill=fill, angle=f&quot;{opr}{angle}&quot;)
  
  
      def replace(self, lst, x_lst, con_lst, text, color, opr):
          self.delete_all(lst, x_lst)
          self.canvas.delete(&quot;all&quot;)
          self.draw_line()
          
          if lst != []:
              self.replace_text(lst, text, color, opr)
  
          elif con_lst != []:
              self.replace_text(con_lst, text, color, opr)
  
  
      def move_line_right(self, event):
          self.canvas.move(self.line, 5, 0)
          self.w+=5
  
          self.positon = False
          self.txt_lst_x = sorted(self.select_x(self.txt_lst_org))
          self.txt_lst_org = sorted(self.txt_lst_org)
          
          print(&quot;enter right&quot;,self.txt_lst_x, &quot;org&quot;, self.txt_lst_org)
  
          if self.txt_lst_x != []:
              try:
                  if self.w &gt;= self.txt_lst_x[0]:
                      print(&quot;TRUE RIGHT&quot;)
                      self.replace(self.txt_lst_org, self.txt_lst_x, self.txt2_lst_org, 
                          self.text2, self.color2, self.opr1)
              except IndexError:
                  pass
  
      def move_line_left(self, event):
          self.canvas.move(self.line, -5, 0)
          self.w-=5
  
          self.positon = True
          self.txt2_lst_x = sorted(self.select_x(self.txt2_lst_org), reverse=self.positon)
          self.txt2_lst_org = sorted(self.txt2_lst_org, reverse=self.positon)
          print(&quot;enter left&quot;, self.txt2_lst_x, &quot;org&quot;, self.txt2_lst_org)
  
          if self.txt2_lst_x != []:
              try:
                  if self.w &lt;= self.txt2_lst_x[0]:
                      print(&quot;TRUE LEFT&quot;)
                      self.replace(self.txt2_lst_org, self.txt2_lst_x, self.txt_lst_org, 
                          self.text1, self.color1, self.opr2)
              except IndexError:
                  pass
  
  main = Program()
  main.mainloop()
  </code></pre>