zoukankan      html  css  js  c++  java
  • [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()

     效果图

    #

  • 相关阅读:
    python中的编码问题
    CVPR2018 Tutorial 之 Visual Recognition and Beyond
    hdu 1376 Octal Fractions
    hdu 1329 Hanoi Tower Troubles Again!
    hdu 1309 Loansome Car Buyer
    hdu 1333 Smith Numbers
    hdu 1288 Hat's Tea
    hdu 1284 钱币兑换问题
    hdu 1275 两车追及或相遇问题
    hdu 1270 小希的数表
  • 原文地址:https://www.cnblogs.com/ansver/p/10588128.html
Copyright © 2011-2022 走看看