【问题标题】:How to use the Gtk ComboBox in gjs?如何在 gjs 中使用 Gtk 组合框?
【发布时间】:2015-02-26 15:32:24
【问题描述】:

遵循一些PyGTK tutorials,我正在尝试在 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 ();

这里的主要问题是它不显示任何内容,例如组合框是空的。根据教程,“new 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 ();

它的优点是它可以准确地显示一些东西,例如组合框充满了可以选择的列表项 - 但它们都是空的。只是白中的白块。

有什么想法吗?

【问题讨论】:

    标签: combobox gjs


    【解决方案1】:

    可能是多余的,但有效:

    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
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多