5

我正在使用 Glade 3 为我正在开发的 PyGTK 应用程序创建一个 GtkBuilder 文件。它用于管理带宽,所以我有一个 gtk.ComboBox 用于选择要跟踪的网络接口。

如何在运行时向 ComboBox 添加字符串?这是我到目前为止所拥有的:

self.tracked_interface = builder.get_object("tracked_interface")

self.iface_list_store = gtk.ListStore(gobject.TYPE_STRING)
self.iface_list_store.append(["hello, "])
self.iface_list_store.append(["world."])
self.tracked_interface.set_model(self.iface_list_store)
self.tracked_interface.set_active(0)

但是 ComboBox 仍然是空的。我尝试了 RTFM'ing,但结果更加困惑,如果有的话。

干杯。

4

2 回答 2

6

嘿,我实际上可以回答我自己的问题!

您必须在其中添加 gtk.CellRendererText 才能实际呈现:

self.iface_list_store = gtk.ListStore(gobject.TYPE_STRING)
self.iface_list_store.append(["hello, "])
self.iface_list_store.append(["world."])
self.tracked_interface.set_model(self.iface_list_store)
self.tracked_interface.set_active(0)
# And here's the new stuff:
cell = gtk.CellRendererText()
self.tracked_interface.pack_start(cell, True)
self.tracked_interface.add_attribute(cell, "text", 0)

当然,取自PyGTK FAQ

感谢 Joe McBride 更正了示例

于 2009-07-24T10:31:33.490 回答
6

或者您可以自己使用gtk.combo_box_new_text(). 然后,您将能够使用 gtk 快捷方式来追加插入前置删除文本。

combo = gtk.combo_box_new_text()
combo.append_text('hello')
combo.append_text('world')
combo.set_active(0)

box = builder.get_object('some-box')
box.pack_start(combo, False, False)
于 2009-07-24T12:03:09.123 回答