0

In my __init__ function I'm trying to do:

builder = gtk.Builder()
builder.add_from_file('main.glade')
builder.connect_signals(Handle())
self.list = builder.get_objects()

Now I want to add attributes to my class for all the widgets in my gui

for i in self.list:
    name = i.get_name()
    print name
    setattr(self, name, i)

I get:

<gtk.VBox object at 0x2fd5558 (GtkVBox at 0x2499000)>
<gtk.Button object at 0x2fd5580 (GtkButton at 0x240a1a8)>
<gtk.Button object at 0x2fd55a8 (GtkButton at 0x240a130)>
<gtk.Button object at 0x2fd55d0 (GtkButton at 0x240a0b8)>
<gtk.Window object at 0x2fd55f8 (GtkWindow at 0x2495010)>
<gtk.HBox object at 0x2fd5620 (GtkHBox at 0x2499058)>
<gtk.Button object at 0x2fd5648 (GtkButton at 0x240a220)>
<gtk.TextView object at 0x2fd5670 (GtkTextView at 0x249d060)>

And not the specific names I gave those widgets

Do you have ideas on how to initialize all the widgets at one?

mlt
  • 1,595
  • 2
  • 21
  • 52
cpunkzzz
  • 1
  • 1

2 Answers2

1

It seems that there is no way to get a list of widget ids. You can assign object attributes on-the-fly by writing __getattr__

class Settings:
  """Settings dialog"""

  def __init__(self):
    self._log = _log.getChild(self.__class__.__name__)
    self.builder = Gtk.Builder()
    self.builder.add_from_file("settings.glade")
    self.builder.connect_signals(self)

  def __getattr__(self, name):
    self._log.debug("Resolving %s", name)
    obj = self.builder.get_object(name)
    setattr(self, name, obj)
    return obj
mlt
  • 1,595
  • 2
  • 21
  • 52
0

I'm not sure what you mean by "initialize". AFAIK, when Builder loads the glade file, the objects are ready to go. Unless I misunderstand, I think you just need to get the name that Glade uses for the widgets it loads. This is accessed with the Buildable interface as follows:

 name = gtk.Buildable(widget).get_name()
ergosys
  • 47,835
  • 5
  • 49
  • 70
  • Perhaps OP wants to turn widget objects into class members using names as seen in Glade. `i.get_name()` returns class name and not widget id, e.g. to be able to access widget *button1* as *self.button1*. – mlt Feb 09 '13 at 07:35