Blame platform-demos/C/samples/combobox_multicolumn.py

Packit 1470ea
from gi.repository import Gtk
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
actions = [["Select", None],
Packit 1470ea
           ["New", Gtk.STOCK_NEW],
Packit 1470ea
           ["Open", Gtk.STOCK_OPEN],
Packit 1470ea
           ["Save", Gtk.STOCK_SAVE]]
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
Packit 1470ea
        self.set_default_size(200, -1)
Packit 1470ea
        self.set_border_width(10)
Packit 1470ea
Packit 1470ea
        # the data in the model, of type string on two columns
Packit 1470ea
        listmodel = Gtk.ListStore(str, str)
Packit 1470ea
        # append the data
Packit 1470ea
        for i in range(len(actions)):
Packit 1470ea
            listmodel.append(actions[i])
Packit 1470ea
Packit 1470ea
        # a combobox to see the data stored in the model
Packit 1470ea
        combobox = Gtk.ComboBox(model=listmodel)
Packit 1470ea
Packit 1470ea
        # cellrenderers to render the data
Packit 1470ea
        renderer_pixbuf = Gtk.CellRendererPixbuf()
Packit 1470ea
        renderer_text = Gtk.CellRendererText()
Packit 1470ea
Packit 1470ea
        # we pack the cell into the beginning of the combobox, allocating
Packit 1470ea
        # no more space than needed;
Packit 1470ea
        # first the image, then the text;
Packit 1470ea
        # note that it does not matter in which order they are in the model,
Packit 1470ea
        # the visualization is decided by the order of the cellrenderers
Packit 1470ea
        combobox.pack_start(renderer_pixbuf, False)
Packit 1470ea
        combobox.pack_start(renderer_text, False)
Packit 1470ea
Packit 1470ea
        # associate a property of the cellrenderer to a column in the model
Packit 1470ea
        # used by the combobox
Packit 1470ea
        combobox.add_attribute(renderer_text, "text", 0)
Packit 1470ea
        combobox.add_attribute(renderer_pixbuf, "stock_id", 1)
Packit 1470ea
Packit 1470ea
        # the first row is the active one at the beginning
Packit 1470ea
        combobox.set_active(0)
Packit 1470ea
Packit 1470ea
        # connect the signal emitted when a row is selected to the callback
Packit 1470ea
        # function
Packit 1470ea
        combobox.connect("changed", self.on_changed)
Packit 1470ea
Packit 1470ea
        # add the combobox to the window
Packit 1470ea
        self.add(combobox)
Packit 1470ea
Packit 1470ea
    def on_changed(self, combo):
Packit 1470ea
        # if the row selected is not the first one, write on the terminal
Packit 1470ea
        # the value of the first column in the model
Packit 1470ea
        if combo.get_active() != 0:
Packit 1470ea
            print("You chose " + str(actions[combo.get_active()][0]) + "\n")
Packit 1470ea
        return True
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)