Reputation: 1772
I'm doing a program in python using GTK3. I have a need to change the GUI interface depending on what the user needs. If I have a window to which I have added a Gtk.Box, and then put something like a label and a text entry in the box and then when needing to change the interface, delete the Box, does that delete the label and text entry in memory? I'm most interesting in Python, but want to learn C as well. Is the answer different for C?
If it doesn't automatically destroy the Gtk.Box, then that means keeping constant track of every widget in the box and needing to call a widget.destroy() for each one. Seems like a bit of a waste.
Thanks,
Narnie
>>> import gtk
>>> win = gtk.Window()
>>> vbox = gtk.VBox()
>>> win.add(vbox)
>>> label = gtk.Label("Hello, everybody!")
>>> vbox.pack_start(label, True, True, 0)
>>> win.show_all()
>>> vbox.destroy()
Does the vbox.destroy() also destroy the label object?
Upvotes: 2
Views: 2285
Reputation: 57854
When you destroy a container, the widgets inside get their reference count decreased. If a widget's reference count drops to zero, it is destroyed. So if you are not holding any extra references in your code, the widgets will be destroyed when you destroy the container.
In C, there is never any question whether you are holding a reference; if you created the widget and didn't add it to a container yet, or called g_object_ref()
on the widget, then you have a reference. If not, not.
In Python, things are more complicated. If the widget is bound to a name in the Python interpreter, it probably has an extra reference added. If you are doing things interactively in an interpreter like IPython which keeps track of old inputs, then there are probably several references. But you don't need to worry about that in Python; the garbage collector will remove the references when the object is not reachable anymore, even if it's not destroyed when you destroy the container.
Upvotes: 6