1from gi.repository import Gtk
2from gi.repository import Pango
3import sys
4
5books = [["Tolstoy, Leo", "War and Peace", "Anna Karenina"],
6         ["Shakespeare, William", "Hamlet", "Macbeth", "Othello"],
7         ["Tolkien, J.R.R.", "The Lord of the Rings"]]
8
9
10class MyWindow(Gtk.ApplicationWindow):
11
12    def __init__(self, app):
13        Gtk.Window.__init__(self, title="Library", application=app)
14        self.set_default_size(250, 100)
15        self.set_border_width(10)
16
17        # the data are stored in the model
18        # create a treestore with one column
19        store = Gtk.TreeStore(str)
20        for i in range(len(books)):
21            # the iter piter is returned when appending the author
22            piter = store.append(None, [books[i][0]])
23            # append the books as children of the author
24            j = 1
25            while j < len(books[i]):
26                store.append(piter, [books[i][j]])
27                j += 1
28
29        # the treeview shows the model
30        # create a treeview on the model store
31        view = Gtk.TreeView()
32        view.set_model(store)
33
34        # the cellrenderer for the column - text
35        renderer_books = Gtk.CellRendererText()
36        # the column is created
37        column_books = Gtk.TreeViewColumn(
38            "Books by Author", renderer_books, text=0)
39        # and it is appended to the treeview
40        view.append_column(column_books)
41
42        # the books are sortable by author
43        column_books.set_sort_column_id(0)
44
45        # add the treeview to the window
46        self.add(view)
47
48
49class MyApplication(Gtk.Application):
50
51    def __init__(self):
52        Gtk.Application.__init__(self)
53
54    def do_activate(self):
55        win = MyWindow(self)
56        win.show_all()
57
58    def do_startup(self):
59        Gtk.Application.do_startup(self)
60
61app = MyApplication()
62exit_status = app.run(sys.argv)
63sys.exit(exit_status)
64