1from gi.repository import Gtk
2import sys
3
4distros = [["Select distribution"], ["Fedora"], ["Mint"], ["Suse"]]
5
6
7class MyWindow(Gtk.ApplicationWindow):
8
9    def __init__(self, app):
10        Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
11        self.set_default_size(200, -1)
12        self.set_border_width(10)
13
14        # the data in the model, of type string
15        listmodel = Gtk.ListStore(str)
16        # append the data in the model
17        for i in range(len(distros)):
18            listmodel.append(distros[i])
19
20        # a combobox to see the data stored in the model
21        combobox = Gtk.ComboBox(model=listmodel)
22
23        # a cellrenderer to render the text
24        cell = Gtk.CellRendererText()
25
26        # pack the cell into the beginning of the combobox, allocating
27        # no more space than needed
28        combobox.pack_start(cell, False)
29        # associate a property ("text") of the cellrenderer (cell) to a column (column 0)
30        # in the model used by the combobox
31        combobox.add_attribute(cell, "text", 0)
32
33        # the first row is the active one by default at the beginning
34        combobox.set_active(0)
35
36        # connect the signal emitted when a row is selected to the callback
37        # function
38        combobox.connect("changed", self.on_changed)
39
40        # add the combobox to the window
41        self.add(combobox)
42
43    def on_changed(self, combo):
44        # if the row selected is not the first one, write its value on the
45        # terminal
46        if combo.get_active() != 0:
47            print("You chose " + str(distros[combo.get_active()][0]) + ".")
48        return True
49
50
51class MyApplication(Gtk.Application):
52
53    def __init__(self):
54        Gtk.Application.__init__(self)
55
56    def do_activate(self):
57        win = MyWindow(self)
58        win.show_all()
59
60    def do_startup(self):
61        Gtk.Application.do_startup(self)
62
63app = MyApplication()
64exit_status = app.run(sys.argv)
65sys.exit(exit_status)
66