如何删除所有子元素? [英] How to delete all children elements?
本文介绍了如何删除所有子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Python 的 tkinter
库编写基于 GUI 的程序.我面临一个问题:我需要删除所有子元素(不删除父元素,在我的例子中是colorsFrame
).
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
).
我的代码:
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)
我如何实现这一目标?
推荐答案
您可以使用 winfo_children
获取特定小部件的所有子部件的列表,然后您可以对其进行迭代:
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()
这篇关于如何删除所有子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文