我想在tkinter中删除画布的所有元素(入口元素除外)。有什么办法吗?
答案 0 :(得分:0)
一种方法是将每个小部件的引用存储在可独立操作的集合中。在这里,我们分别存储Label
,Button
和Entry
。按下remove all but entry
按钮后,除Entries
import tkinter as tk
def remove_all_but_entries():
for button in buttons:
button.destroy()
for label in labels:
label.destroy()
root = tk.Tk()
labels = []
labels.append(tk.Label(root, text='make disappear example'))
for label in labels:
label.pack()
buttons = []
buttons.append(tk.Button(root, text='start', command=lambda: print('button start')))
buttons.append(tk.Button(root, text='stop', command=lambda: print('button stop')))
buttons.append(tk.Button(root, text='remove all but entry', command=remove_all_but_entries))
for button in buttons:
button.pack()
entries = []
entries.append(tk.Entry(root))
for entry in entries:
entry.pack()
root.mainloop()