1

In GTK3, how can I use gtk_*_new_*() constructors?

For instance, consider:

GtkWidget * gtk_recent_chooser_menu_new_for_manager () 

How can I use it?

I am trying to port a PyGTK2 program, and I can workaround things like gtk_text_view_new_with_buffer() by instead doing:

view = Gtk.TextView() view.set_buffer(buffer) 

Is it possible to use such functions directly?

2 Answers 2

1

tl;dr: Use the Python constructor normal form to set construction properties instead of the convenience C API.

The various _new constructors in the C API, including the basic _new() one, are just wrappers around g_object_new(). Constructors like type_new_with_foo() are just convenience functions for C developers to give some type safety around a call to g_object_new() and property setting; so, for instance, gtk_recent_chooser_menu_new() is just:

return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU, NULL); 

and gtk_recent_chooser_menu_new_for_manager() is:

return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU, "recent-manager", manager, NULL); 

i.e. a way to set the (constructor-only) "recent-manager" property.

The Python bindings do not call the C API for constructing objects; the Python constructor normal form, e.g.:

button = Gtk.Button() 

is the equivalent of calling g_object_new(GTK_TYPE_BUTTON, NULL). The constructor normal form accepts arguments, in the form of a dictionary of property names and values:

button = Gtk.Button(label='Hello, world') 

This is the equivalent of calling gtk_button_new_with_label() in C. The Python bindings layer will provide all the necessary type safety at run time.

It's usually recommended to use this approach, and not calling the C API because:

  • it's more Pythonic, as it follows the conventions of the language
  • it's more flexible, as it lets you set more properties without requiring additional C API
Sign up to request clarification or add additional context in comments.

1 Comment

That was a superb description of the constructors in bindings. Meanwhile, I open the following related question. Do you think you can help? stackoverflow.com/questions/38171196/…
1

I figure it out.

Apparently you can just use things like:

view = Gtk.TextView.new_with_buffer(buffer) 

Interestingly, Gtk.TextView.new() == Gtk.TextView()

4 Comments

I don't know if Python requires those functions. The constructor (*new()) should be doing extra work but perhaps the binding handles that anyway. C, C++, etc. however require these functions, because you create a GtkWidget and then instantiate it with the constructor. On the other hand, you have typeless languages like Perl, where you use the functions to differentiate between different types of widgets.
@oldtechaa In Python, Gtk.Button() is the same as Gtk.Button.new(). If you want to use a custom new_*() you should reference it directly. [reference needed -- but I don't remember where I found this hehe]
Sounds like the binding handles it for you. That's a pretty good binding then. It spoils Python people though not to need their constructors. :) Perl, on the other hand, sticks close to the C style except for typing, but also is a good easy binding.
@oldtechaa GTK+ has many flaws, but, indeed, the bindings are pretty cool.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.