20
votes

I am writing a GUI-based program using Python's tkinter library. I am facing a problem: I need to delete all children elements (without deleting a parent element, which in my case is colorsFrame).

My code:

infoFrame = Frame(toolsFrame, height = 50, bd = 5, bg = 'white')
colorsFrame = Frame(toolsFrame)

# adding some elements

infoFrame.pack(side = 'top', fill = 'both')
colorsFrame.pack(side = 'top', fill = 'both')

# set the clear button
Button(buttonsFrame, text = "Clear area",
               command = self.clearArea).place(x = 280, y = 10, height = 30)

How do I achieve this?

1

1 Answers

34
votes

You can use winfo_children to get a list of all children of a particular widget, which you can then iterate over:

for child in infoFrame.winfo_children():
    child.destroy()