如何在GJ中使用GTK组合

  • 本文关键字:GTK 组合 GJ combobox gjs
  • 更新时间 :
  • 英文 :


遵循一些pygtk教程,我正在尝试填充gjs中的组合盒(gnome桌面上的本机javascript)

到目前为止,我想出了两种类似的方式,两种类似的方式

第一个可能最接近教程中的示例:

var testStore = new Gtk.ListStore ();
testStore.append ([0, "test1"]);
testStore.append ([1, "test2"]);
var cbox = Gtk.ComboBox.new_with_model (testStore);
cbox.set_entry_text_column (1);
cbox.show ();

主要问题是它没有显示任何内容,例如ComboBox是空的。根据教程,"新的gtk.liststore"需要列类型作为参数,但是我放在那里的任何东西都会引起一些错误消息。

将其与其他示例中的代码混合,我想到了这个:

var testStore = new Gtk.ListStore ();
testStore.append ([0, "test1"]);
testStore.append ([1, "test2"]);
var cbox = Gtk.ComboBox.new_with_model (testStore);
var cellRenderer = new Gtk.CellRendererText ();
cbox.pack_start (cellRenderer, true);
cbox.add_attribute (cellRenderer, "text", 1);
cbox.show ();

它具有一个优势,即它在Acutsing显示某些内容,例如Combobox填充了可以选择的列表项目 - 但它们都是空的。只是白色的白色。

有什么想法?

也许是多余的,但有效:

let model = new Gtk.ListStore();
model.set_column_types([GObject.TYPE_STRING, GObject.TYPE_STRING]);
let cbox = new Gtk.ComboBox({model: model});
let renderer = new Gtk.CellRendererText();
cbox.pack_start(renderer, true);
cbox.add_attribute(renderer, 'text', 1);
model.set(model.append(), [0, 1], ['key1', 'value1']);
model.set(model.append(), [0, 1], ['key2', 'value2']);
cbox.set_active(0); // set value
cbox.connect('changed', function(entry) {
    let [success, iter] = cbox.get_active_iter();
    if (!success)
        return;
    let myValue = model.get_value(iter, 0); // get value
});

最新更新