[tkinter]隐藏/销毁控件

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()
View Code
复制代码

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()
复制代码

 效果图

 

 

 

#

posted @   ansver  阅读(19138)  评论(0编辑  收藏  举报
编辑推荐:
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
阅读排行:
· BotSharp + MCP 三步实现智能体开发
· 动物智能之数据标注员——狗篇
· BotSharp 5.0 MCP:迈向更开放的AI Agent框架
· 5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
· 设计模式脉络
点击右上角即可分享
微信分享提示