10

How do I get a widget's "name"?

When I define a GUI using Glade, I can "name" the widgets of the window but how do I recover that property when I have a widget object instance?

I've tried get_property(), get_name() and $widget.name to no avail.

Update: I am using GtkBuilder file format (i.e. XML format).

Resolution: a fix I have used: use the set_property("name", name) method on the widget just after getting it from GtkBuilder.

1
  • What format are you using? Glade or GTKBuilder? Commented Aug 16, 2010 at 1:48

3 Answers 3

15

I use the class-method form, which works for me:

gtk.Buildable.get_name(widget)

References

PyGtk: gtk.Buildable.get_name

Gtk3: Gtk.Buildable.get_name

Quote from gtk2 GtkBuilder:

Prior to GTK+ 2.20, gtk.Builder was setting the "name" property of constructed widgets to the "id" attribute. In GTK+ 2.20 or newer, you have to use gtk.Buildable.get_name() instead of gtk.Widget.get_name() to obtain the "id", or set the "name" property in your UI definition.


Further

There were some reported bugs where GtkBuilder sets the widget name to be the builder ID, or doesn't set it at all. See Ubuntu and GNOME bugs. I have no idea why the bugs says that it sets the ID instead of the name as I've only ever seen the name not set.

Sign up to request clarification or add additional context in comments.

1 Comment

that's exactly what has been happening on my end! Thanks for the heads-up!
2
self.builder.get_name(widget)

Where widget is the widget object. Works using Ubuntu's packaged quickly development environment

1 Comment

Afraid this answer is incomplete and misleading, what is self.builder variable? gtk.Builder (gtk2 nor gtk3) doesn't have a get_name method.
0

Given the widget w, what does w.get_name() return? If None, that means the widget has no name property. Maybe you want gtk.glade.get_widget_name(w) instead? (I'm not sure if the name property of the widget and the name for it in the Glade XML from which it was created are the same thing...).

2 Comments

They return "None", as well as gtk.glade.get_widget_name(w).
@jdupont, and yet you're certain w was built from a named entry in a Glade XML file? Then I'm out of ideas...:-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.