Python: removing a TKinter frame

前端 未结 4 1164
走了就别回头了
走了就别回头了 2021-02-06 07:35

I want to remove a frame from my interface when a specific button is clicked.

This is the invoked callback function

def removeMyself(self):
    del self
         


        
4条回答
  •  面向向阳花
    2021-02-06 08:35

    Let's say you're making a class. You have to do a couple of things special here:

    • The frame you want to destroy has to be an instance variable
    • You have to write a callback (which you did)

    So, here's how a basic prototype would look.

    from Tkinter import Tk, Frame, Button, Label
    
    class GUI:
    
        def __init__(self, root):
            self.root = root # root is a passed Tk object
            self.button = Button(self.root, text="Push me", command=self.removethis)
            self.button.pack()
            self.frame = Frame(self.root)
            self.frame.pack()
            self.label = Label(self.frame, text="I'll be destroyed soon!")
            self.label.pack()
    
        def removethis(self):
            self.frame.destroy()
    
    root = Tk()
    window = GUI(root)
    root.mainloop()
    

    Happy hunting!

提交回复
热议问题