aneuryzm
aneuryzm

Reputation: 64834

Python: removing a TKinter frame

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

However, it doesn't remove itself. I'm probably just deleting the object in python without updating the interface ?

thanks

Update

self.itemFrame = tk.Frame(parent)
self.itemFrame.pack(expand=False, side=tk.TOP)

removeB = tk.Button(self.itemFrame, text="Remove", width=10, command=self.removeIsosurface)

def removeIsosurface(self):
    self.itemFrame.Destroy()

Error message:

AttributeError: Frame instance has no attribute 'Destroy'

Upvotes: 10

Views: 91659

Answers (5)

Nikish Daniel
Nikish Daniel

Reputation: 1

Hey can you try doing that by using global variable #set a variable decide= 0 decide= 0

def screen():
    def showframe():
        global decide
        if decide == 0:
            frame = frame(main).pack()
        else:
            decide = 0
            for i in main.winfo_children():
                i.destroy()
            screen()
            main = TK()
            screen() 
            main.mainloop()

Upvotes: 0

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

To remove, call either frm.pack_forget() or frm.grid_forget() depending on whether the frame was packed or grided.

Then call frm.destroy() if you aren't going to use it again, or hold onto the reference and repack or regrid when you want to show it again.

Upvotes: 26

Rafe Kettler
Rafe Kettler

Reputation: 76955

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!

Upvotes: 1

jknair
jknair

Reputation: 4764

wont this help : self.destroy()

chk this out : PY cookbook the last para

Upvotes: 0

user395760
user395760

Reputation:

del does not delete anything. del something just removes something from the local scope. And although if something was the only reference to an object, it may allow the object it to be garbage collected in the future, don't even think of using del to delete objects!!! And since self is just a normal variables, del self does nothing, except of course stopping the rest of the method from accessing the instance (so at the end of the method, it's actually like pass).

The exact way to remove a widget from the GUI depends on what geometry manager you use. If you used .grid(), you can use .grid_forget(). Note that this still doesn't destroy the widget - quite the contrary, you can go on and .grid() it again! - but that doesn't make any difference.

Upvotes: 2

Related Questions