Access to widget in GTK+
Building a GTK+ widget dynamically from code allows for easy access to the child widg开发者_运维技巧ets directly.
Now, how do I access to the child widgets when building a GTK+ Dialog (as example) from a .glade
file?
class ConfigDialog(object):
def __init__(self, glade_file, testing=False):
self.testing=testing
builder = gtk.Builder()
builder.add_from_file(glade_file)
self.dialog = builder.get_object("config_dialog")
self.dialog._testing=testing
self.dialog._builder=builder
I've tinkering a bit with .get_internal_child
without success.
Q: let's say I want to access the widget named "name_entry", how would I go about it?
Already you are making the call
self.dialog = builder.get_object("config_dialog")
You should also be able to do
self.nameEntry = builder.get_object("name_entry")
This is at least how python-glade works and I assume GtkBuilder is the same.
In addition, if you want to search for a named widget and the Builder instance isn't available, you could try using the following utility function:
def get_child_by_name(parent, name):
"""
Iterate through a gtk container, `parent`,
and return the widget with the name `name`.
"""
def iterate_children(widget, name):
if widget.get_name() == name:
return widget
try:
for w in widget.get_children():
result = iterate_children(w, name)
if result is not None:
return result
else:
continue
except AttributeError:
pass
return iterate_children(parent, name)
精彩评论