Python: removing a TKinter frame
Asked Answered
S

5

10

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'
Salvia answered 18/10, 2010 at 18:38 Comment(2)
del self just removes the name self from the local scope.Sukkah
Bases on the indention you have shown it appears you wrote a method as a sub method to the init method. self.itemFrame and removeB should both be indented out and not at the same level as the method below them. This could explain why you are not able to destroy the frame.Florencia
A
25

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.

Annelleannemarie answered 18/10, 2010 at 19:41 Comment(3)
Finally, pack_forget make it disappear! I still have an issue with destroy method. I get the error message you see in the questionSalvia
destroy is all lower case. Error message shows that you used upper case.Annelleannemarie
Steven Rumbalski I got the same error with lowercase "destroy()". Actually the code is working with just pack_forget. But I need destroy to clean the memory I guess..Salvia
C
2

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.

Complement answered 18/10, 2010 at 19:2 Comment(3)
ok thanks, but I didn't use grid. My widget is a Frame. How can I delete frames ?Salvia
@Patrick: I know you use a Frame - I'm talking about the geometry manager. I.e. did you call the_frame.grid(some, options) in order to show the widget?Complement
No, again, I didn't call grid. I've added the code to my questionSalvia
B
1

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!

Bureaucrat answered 18/10, 2010 at 19:43 Comment(4)
This is exactly what I have, but I still get the error "frame has not attribute Destroy()". I've updated my question with the codeSalvia
And you put the whole GUI in a class? Also, what version of Python are you using?Bureaucrat
Yes, it is in a class. I'm using Python 2.6.1Salvia
That's strange. This script runs fine for me in Python 2.7. Not sure what's going on on your end.Bureaucrat
B
0

wont this help : self.destroy()

chk this out : PY cookbook the last para

Biernat answered 18/10, 2010 at 19:1 Comment(1)
my widget is a Frame, it says that it doesn't have any attribute destroySalvia
P
0

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()
Perigordian answered 8/12, 2023 at 11:53 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Bedroom

© 2022 - 2024 — McMap. All rights reserved.