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()