1from gi.repository import Gtk
2import sys
3
4actions = [["Select", None],
5           ["New", Gtk.STOCK_NEW],
6           ["Open", Gtk.STOCK_OPEN],
7           ["Save", Gtk.STOCK_SAVE]]
8
9
10class MyWindow(Gtk.ApplicationWindow):
11
12    def __init__(self, app):
13        Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
14        self.set_default_size(200, -1)
15        self.set_border_width(10)
16
17        # the data in the model, of type string on two columns
18        listmodel = Gtk.ListStore(str, str)
19        # append the data
20        for i in range(len(actions)):
21            listmodel.append(actions[i])
22
23        # a combobox to see the data stored in the model
24        combobox = Gtk.ComboBox(model=listmodel)
25
26        # cellrenderers to render the data
27        renderer_pixbuf = Gtk.CellRendererPixbuf()
28        renderer_text = Gtk.CellRendererText()
29
30        # we pack the cell into the beginning of the combobox, allocating
31        # no more space than needed;
32        # first the image, then the text;
33        # note that it does not matter in which order they are in the model,
34        # the visualization is decided by the order of the cellrenderers
35        combobox.pack_start(renderer_pixbuf, False)
36        combobox.pack_start(renderer_text, False)
37
38        # associate a property of the cellrenderer to a column in the model
39        # used by the combobox
40        combobox.add_attribute(renderer_text, "text", 0)
41        combobox.add_attribute(renderer_pixbuf, "stock_id", 1)
42
43        # the first row is the active one at the beginning
44        combobox.set_active(0)
45
46        # connect the signal emitted when a row is selected to the callback
47        # function
48        combobox.connect("changed", self.on_changed)
49
50        # add the combobox to the window
51        self.add(combobox)
52
53    def on_changed(self, combo):
54        # if the row selected is not the first one, write on the terminal
55        # the value of the first column in the model
56        if combo.get_active() != 0:
57            print("You chose " + str(actions[combo.get_active()][0]) + "\n")
58        return True
59
60
61class MyApplication(Gtk.Application):
62
63    def __init__(self):
64        Gtk.Application.__init__(self)
65
66    def do_activate(self):
67        win = MyWindow(self)
68        win.show_all()
69
70    def do_startup(self):
71        Gtk.Application.do_startup(self)
72
73app = MyApplication()
74exit_status = app.run(sys.argv)
75sys.exit(exit_status)
76