我有一个框架,其中我试图删除所有子小部件,特别是其中一个。我熟悉删除所有具有以下内容的孩子:
for child in frame.winfo_children():
child.destroy()
我什至可以阻止通过以下方式删除特定类型的小部件(例如Button):
for child in frame.winfo_children():
if child.winfo_class() != 'Button':
child.destroy()
我想要的是能够删除所有类型的所有子代,除了1个特定的小部件。这是下面的全功能示例。我想销毁keep_me
以外的所有东西。
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root)
frame.grid(column=0, row=0)
keep_me = tk.Button(frame, text='Keep me')
keep_me.grid(column=0, row=0)
destroy_me = tk.Button(frame, text='Expendable')
destroy_me.grid(column=0, row=1)
for child in frame.winfo_children():
# Here's where I need help. Don't know how to single-out 'keep_me'
child.destroy()
答案 0 :(得分:3)
您仅可以查看用于保存窗口小部件的变量是否等于子变量,即。
for child in frame.winfo_children():
# Here's where I need help. Don't know how to single-out 'keep_me'
if child != keep_me:
child.destroy()