pack布局的情况下有pack_forget()方法让控件“不再显示”但控件还存在可以再次pack出来
from tkinter import *
root = Tk()
l1 = Label(root, text='pack_forget')
b3 = Button(root, text='按钮')
b1 = Button(root, text='隐藏', command=b3.pack_forget)
b2 = Button(root, text='显示', command=b3.pack)
l1.pack(fill=X)
b1.pack(fill=X)
b2.pack(fill=X)
b3.pack()
root.mainloop()
grid,place布局下也有对应的grid_forget(),place_forget()
这个控件只是不再显示,但依然存在 在内存里 !!
还有一个destroy(),但是这个是“销毁”,是无法重现的
除非控件不再用了,或者是想释放内存,否则不要destroy
from tkinter import *
root = Tk()
button_list = []
def add_button():
b = Button(root, text='按钮')
b.pack(fill=X)
button_list.append(b)
def reduce_button():
if button_list:
b = button_list.pop()
# b.pack_forget()
b.destroy()
b1 = Button(root, text='添加按钮', command=add_button)
b2 = Button(root, text='减少按钮', command=reduce_button)
b1.pack()
b2.pack()
root.mainloop()
效果图
#