9

I'm using a custom container where I need to reorder widgets but there are no methods to do it. So I tried to remove all the widgets and add them again in order.

The problem is that this is not working well, I can't see the widgets after adding them again, I guess what's happening is that when I remove the widgets they become unrealized.

Is there any way of removing a widget an reuse it later?

pmoleri
  • 4,238
  • 1
  • 15
  • 25

3 Answers3

9

The pygtk docs provide a bit of insight.

Note that the container will own a reference to widget, and that this may be the last reference held; so removing a widget from its container can cause that widget to be destroyed. If you want to use widget again, you should add a reference to it.

EDITS

I just quickly modified pygtk's Hello World to add/remove/reorder widgets in a container. This works because the button1 is a member variable of the class, it never goes out of scope.

#!/usr/bin/env python

# example helloworld2.py

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld2:

    # Our new improved callback.  The data passed to this method
    # is printed to stdout.
    def callback_remove(self, widget, data):
    self.box1.remove(self.button1);

    def callback_add(self, widget, data):
        self.box1.pack_start(self.button1, True, True, 0)

    # another callback
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        # This is a new call, which just sets the title of our
        # new window to "Hello Buttons!"
        self.window.set_title("Hello Buttons!")

        # Here we just set a handler for delete_event that immediately
        # exits GTK.
        self.window.connect("delete_event", self.delete_event)

        # Sets the border width of the window.
        self.window.set_border_width(10)

        # We create a box to pack widgets into.  This is described in detail
        # in the "packing" section. The box is not really visible, it
        # is just used as a tool to arrange widgets.
        self.box1 = gtk.HBox(False, 0)

        # Put the box into the main window.
        self.window.add(self.box1)

        # Creates a new button with the label "Button 1".
        self.button1 = gtk.Button("Button 1")

        # Now when the button is clicked, we call the "callback" method
        # with a pointer to "button 1" as its argument
        self.button1.connect("clicked", self.callback_remove, "button 1")

        # Instead of add(), we pack this button into the invisible
        # box, which has been packed into the window.
        self.box1.pack_start(self.button1, True, True, 0)

        # Always remember this step, this tells GTK that our preparation for
        # this button is complete, and it can now be displayed.
        self.button1.show()

        # Do these same steps again to create a second button
        self.button2 = gtk.Button("Button 2")

        # Call the same callback method with a different argument,
        # passing a pointer to "button 2" instead.
        self.button2.connect("clicked", self.callback_add, "button 2")

        self.box1.pack_start(self.button2, True, True, 0)

        # The order in which we show the buttons is not really important, but I
        # recommend showing the window last, so it all pops up at once.
        self.button2.show()
        self.box1.show()
        self.window.show()

def main():
    gtk.main()

if __name__ == "__main__":
    hello = HelloWorld2()
    main()
Jason
  • 2,493
  • 2
  • 27
  • 27
Mark
  • 106,305
  • 20
  • 172
  • 230
  • Yes I read that information, but I don't know if I can manually add references to my widget. The widget keeps referenced by python but not by GTK. – pmoleri Apr 10 '11 at 22:07
  • @pmoleri, I just added a quick and dirty example. It can remove and re-add widgets to a container (an HBox) pretty easily. Clicking "Button 1" will remove it, then clicking "Button 2" will add it back on the end of the container. I think the "adding a reference" just means, you can not let it go out of scope. – Mark Apr 10 '11 at 22:25
3

Just set the widget's visibility property to False, and set it to True later, with the set_visible method.

Flimm
  • 136,138
  • 45
  • 251
  • 267
1

no he need only widgrt.its objects not need.then visible must not used.it will only hide graphicaly.but memmory still not released

remove function is the answer

aovbros
  • 11
  • 1