Home > Software design >  tkinter: cleanly destroy auto-wrapping widgets created in tk.Text with window_create
tkinter: cleanly destroy auto-wrapping widgets created in tk.Text with window_create

Time:05-30

Based on enter image description here

How can I cleanly remove the associated window so that they don't add up over time?

CodePudding user response:

You need to remove the items from the Text widget using the index returned by .dump(). But you need to remove them in reverse order, otherwise the index will be wrong after removing the first item.

def delete_labels():
    for lbl in label_text_field.dump("1.0", "end")[::-1]:  # get the items in reverse order
        if lbl[0] =='window' and lbl[1]:
            label_text_field.nametowidget(lbl[1]).destroy()
            label_text_field.delete(lbl[2]) # remove item from text box as well

Actually you can pass window=1 to dump() to return window items only:

def delete_labels():
    for lbl in label_text_field.dump("1.0", "end", window=1)[::-1]:
        if lbl[1]:
            label_text_field.nametowidget(lbl[1]).destroy()
            label_text_field.delete(lbl[2])
  • Related