title: How do I get a Canvas object's ID so I can delete it in tkinter?
body: <p>I'm just learning some tkinter, just trying to get the feel of it.</p>
  <p>In my simple window is a canvas in a frame. There is an input field. Write some text, click the button and the text will appear below the button.</p>
  <p>There are 4 labels in the canvas. I want to get the object ID of label4, so I can delete it each time around, then recreate label4. Otherwise, the new text is on top of the old text in label4.</p>
  <p>Someone told me use:</p>
  <blockquote>
  <p>canvas1.delete(&quot;all&quot;)</p>
  </blockquote>
  <p>but that deletes all labels and my button. (also tried canvas1.delete(label4) and canvas1.delete(&quot;label4&quot;) but didn't seem to work.)</p>
  <p>I read that I need the object's ID to delete it, which is just an integer, so I guessed label4's ID would be 4. I set:</p>
  <blockquote>
  <p>canvas1.delete(4)</p>
  </blockquote>
  <p>Which got rid of label4 the first time, before the function getInput() recreates it. But that only works 1 time. A new entry in the input field and a click of the button recreates label4, but the ID is no longer 4, because the second time around, label4's text stays.</p>
  <p><strong>How do I get a canvas object's ID so that I can specifically delete that object?</strong></p>
  <pre><code>def myWindow1():
      window = tkinter.Tk()
      window.title(&quot;Get a text input and echo it&quot;)
      window.config(bg='light blue')
      window.geometry('640x480')
      # make a frame to take the canvas
      frame=Frame(window,width=400,height=300, bg='lightblue')
      frame.pack(expand=True, fill=BOTH) #.grid(row=0,column=0)
  
      xscrollbar = Scrollbar(frame, orient=HORIZONTAL, width=20, activebackground='red', )
      yscrollbar = Scrollbar(frame, orient=VERTICAL, width=20, activebackground='green', )
      xscrollbar.pack( side = BOTTOM, fill = X )
      yscrollbar.pack( side = RIGHT, fill = Y )
  
      #canvas1 = tkinter.Canvas(window, xscrollcommand = xscrollbar.set, yscrollcommand = yscrollbar.set, bg='white', width = 400, height = 300,  relief = 'raised')
      #canvas1.pack()
      canvas1 = tkinter.Canvas(frame, bg='white', width = 400, height = 300,  relief = 'raised')
      canvas1.config(xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
      canvas1.pack(padx=10, pady=10)
  
      label1 = tkinter.Label(frame, text='Echo a text input')
      label1.config(font=('helvetica', 14))
      canvas1.create_window(200, 25, window=label1)
  
      label2 = tkinter.Label(frame, text='Type your text:')
      label2.config(font=('helvetica', 10))
      canvas1.create_window(200, 100, window=label2)
  
      entry1 = tkinter.Entry (frame) 
      canvas1.create_window(200, 140, window=entry1)
  
      def getInput():
          
          canvas1.delete(4)
          #label4.destroy
          msg = entry1.get()
          
          label3 = tkinter.Label(frame, text= 'You entered this text:',font=('helvetica', 10))
          canvas1.create_window(200, 210, window=label3)
          # wraplength is in pixels not characters!!
          #label4 = tkinter.Label(window, text=msg,font=('helvetica', 10, 'bold'), anchor='w', width=35, wraplength=80)
          #label4 = tkinter.Label(frame, text=msg,font=('helvetica', 10, 'bold'), anchor='w', width=55, wraplength=390)
          label4 = tkinter.Label(frame, text=msg,font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
          #canvas1.update()
          canvas1.create_window(200, 250, window=label4)
          # don't set the y value too high or you won't see the output!
          #canvas1.create_window(200, 320, window=label4)
  
      global label4
      label4 = tkinter.Label(frame, text= 'Your input will be here after you click the button.',font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
      canvas1.create_window(200, 250, window=label4)    
      button1 = tkinter.Button(text='Get the text message', command=getInput, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
      canvas1.create_window(200, 180, window=button1)
      
      window.mainloop()
  </code></pre>