1

I know it's possible to import specific objects from a glade file using:

builder.add_objects_from_file("example.glade", ("button1", "button2"))

But as you can see, I have to pass a list of the objects I want to import.

Is there a way to import everything in the .glade file? All the objects without having to specify their names here?

Madno
  • 910
  • 2
  • 12
  • 27

1 Answers1

1

When you use gtk_builder_add_objects_from_file, you are merging specific objects to an existing GtkBuilder instance which will then instantiate those same objects. Using this function/method is only useful if you need a specific set of objects from a UI definition file.

The normal use of GtkBuilder, would be to load it entirely and then retrieve the object you want to deal with gtk_builder_get_object. But if your goal is to retrieve all the objects, then use gtk_builder_get_objects which will return a GSList. Using this function/method assumes that you already loaded a UI definition file from a file or from another possible source.

As documented:

GSList *gtk_builder_get_objects (GtkBuilder *builder);

Gets all objects that have been constructed by builder . Note that this function does not increment the reference counts of the returned objects.

Returns

a newly-allocated GSList containing all the objects constructed by the GtkBuilder instance. It should be freed by g_slist_free().

José Fonte
  • 4,016
  • 2
  • 16
  • 28
  • I am aware of this approach but for some reason it's not working with me. I wrote: builder = Gtk.Builder();builder.add_from_file("ui/myui.glade");builder.get_objects(). But when I try to use a method for an object I made in the glade file (like if I created a window, and named it window1), Python will tell me: NameError: name 'window1' is not defined – Madno Jun 05 '17 at 20:42
  • @Madno Is there a reason to get all objects at the same time? – José Fonte Jun 05 '17 at 20:49
  • Yes. Instead of using something like "window = gtk_builder_get_object("window1")" for each object and thing I have in the glade file, I want to make it in few lines. Which is why I am asking about a way to import all the objects, then, I can use something like window1.set_title("title") directly. – Madno Jun 05 '17 at 20:51
  • 1
    @Madno Hmmm it doesnt work that way. When you create a UI via glade, there will be a lot of widgets that you don't need to use programmatically, so getting a pointer to them its completely redundant and irrelevant. Once you have your GtkBuilder filled with data from a file, it will instantiate all the widgets (it's like a "automatic" factory pattern). Then you need to retrieve the widgets you want to use, when you need them. For that you use the normal methodology, button = builder.get_object ("button1") and then button.set_label(.... – José Fonte Jun 05 '17 at 20:58
  • @Madno if you use get_objects method you will then have a linked list of object/widgets on which you must iterate to get the exact object you want and even then you need a "pointer" to the widget/object if you want to reuse it. So, do as normal... keep the builder as a property on your python class and you will be able to retrieve the widgets/objects when and where you need them inside that same python class/object scope. Did you understand or i've made you more confused? – José Fonte Jun 05 '17 at 21:01
  • Yea I understood :) Thank you. – Madno Jun 05 '17 at 21:05
  • Nice :) Feel free to ask more questions if you need. Good luck with Gtk ;) – José Fonte Jun 05 '17 at 21:06