Question

I have a combobox widget in python that I would like to be able to choose multiple items, but I'm starting to think this is not possible. I see that it is possible on using Gtk.TreeView() by setting the mode to multiple. Is there a way to get a combobox to do this? If no, can a treeview be placed inside a combobox and if so how (short coding example, please)? I'm using GTK3, but I could probably translate it from a GTK2 example.

Was it helpful?

Solution

After much research, I think it is simply a limitation of the combobox that it can only hold one item. So, the answer is:

Yes, a combobox can be set up to select multiple (if it has a TreeView in it)

and, thus,

Yes, a TreeView can be placed inside a ComboBox.

BUT, it doesn't behave correctly as the ComboBox acts as a container with the TreeView always visible, not just when activating the ComboBox. It can be set to select multiple useing Gtk.TreeSelection (gotten from Gtk.Treeview.get_selection()).

Here is the code:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

from gi.repository import Gtk

PEOPLE =    [
            "Frank",
            "Martha",
            "Jim Bob",
            "Francis"
            ]

class TreeCombo(object):
    def __init__(self):
        self.win = Gtk.Window(title="Combo with liststore")
        self.win.connect('delete-event', Gtk.main_quit)

        self.store = Gtk.ListStore(str)
        for person in PEOPLE:
            self.store.append([person])

        # self.combo = Gtk.ComboBox.new_with_model(self.store)
        self.combo = Gtk.ComboBox()

        self.tree = Gtk.TreeView(self.store)
        self.selector = self.tree.get_selection()
        self.selector.set_mode(Gtk.SelectionMode.MULTIPLE)

        self.combo_cell_text = Gtk.CellRendererText()

        self.column_text = Gtk.TreeViewColumn("Text", self.combo_cell_text, text=0)

        self.tree.append_column(self.column_text)

        self.combo.add(self.tree)

        self.win.add(self.combo)

        self.win.show_all()




def main():
    prog = TreeCombo()
    Gtk.main()

if __name__ == "__main__":
    main()

I'm going to mess around with hiding and showing the treeview with activation of the combobox. I'll let you know how it goes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top