21

我正在使用 Python 的tkinter库编写一个基于 GUI 的程序。我面临一个问题:我需要删除所有子元素(不删除父元素,在我的情况下是colorsFrame)。

我的代码:

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)

我如何实现这一目标?

4

1 回答 1

40

您可以使用winfo_children获取特定小部件的所有子级的列表,然后可以对其进行迭代:

for child in infoFrame.winfo_children():
    child.destroy()
于 2013-04-14T04:49:06.177 回答