Pedroski
Pedroski

Reputation: 487

How do I get a Canvas object's ID so I can delete it in tkinter?

I'm just learning some tkinter, just trying to get the feel of it.

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.

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.

Someone told me use:

canvas1.delete("all")

but that deletes all labels and my button. (also tried canvas1.delete(label4) and canvas1.delete("label4") but didn't seem to work.)

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:

canvas1.delete(4)

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.

How do I get a canvas object's ID so that I can specifically delete that object?

def myWindow1():
    window = tkinter.Tk()
    window.title("Get a text input and echo it")
    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()

Upvotes: 1

Views: 1101

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386210

How do I get a Canvas object's ID so I can delete it in tkinter?

The identifier is returned when you create an object on the canvas:

the_id = canvas1.create_window(...)
...
canvas1.delete(the_id)

Upvotes: 2

Related Questions