我正在使用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)
Run Code Online (Sandbox Code Playgroud)
但是ComboBox仍然是空的.我尝试过RTFM,但是如果有的话,就会更加困惑.
干杯.
嘿,我实际上回答了我自己的问题!
您必须将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)
Run Code Online (Sandbox Code Playgroud)
当然,取自PyGTK常见问题解答.
修正了Joe McBride的例子
或者您可以自己创建并插入组合框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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11569 次 |
最近记录: |