在我试图做的__init__
函数中:
builder = gtk.Builder()
builder.add_from_file('main.glade')
builder.connect_signals(Handle())
self.list = builder.get_objects()
现在我想在我的类中为我的gui
中的所有小部件添加属性for i in self.list:
name = i.get_name()
print name
setattr(self, name, i)
我明白了:
<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)>
而不是我给这些小部件的具体名称
您是否有关于如何在一个小部件初始化所有小部件的想法?
答案 0 :(得分:1)
似乎无法获取小部件ID列表。您可以通过编写__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
答案 1 :(得分:0)
我不确定你的意思是“初始化”。 AFAIK,当Builder加载glade文件时,对象就可以运行了。除非我误解,否则我认为你只需要获得Glade用于它加载的小部件的名称。可以使用Buildable接口访问它,如下所示:
name = gtk.Buildable(widget).get_name()