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

Packit 1470ea
from gi.repository import Gtk
Packit 1470ea
from gi.repository import Pango
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
books = [["Tolstoy, Leo", "War and Peace", "Anna Karenina"],
Packit 1470ea
         ["Shakespeare, William", "Hamlet", "Macbeth", "Othello"],
Packit 1470ea
         ["Tolkien, J.R.R.", "The Lord of the Rings"]]
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="Library", application=app)
Packit 1470ea
        self.set_default_size(250, 100)
Packit 1470ea
        self.set_border_width(10)
Packit 1470ea
Packit 1470ea
        # the data are stored in the model
Packit 1470ea
        # create a treestore with one column
Packit 1470ea
        store = Gtk.TreeStore(str)
Packit 1470ea
        for i in range(len(books)):
Packit 1470ea
            # the iter piter is returned when appending the author
Packit 1470ea
            piter = store.append(None, [books[i][0]])
Packit 1470ea
            # append the books as children of the author
Packit 1470ea
            j = 1
Packit 1470ea
            while j < len(books[i]):
Packit 1470ea
                store.append(piter, [books[i][j]])
Packit 1470ea
                j += 1
Packit 1470ea
Packit 1470ea
        # the treeview shows the model
Packit 1470ea
        # create a treeview on the model store
Packit 1470ea
        view = Gtk.TreeView()
Packit 1470ea
        view.set_model(store)
Packit 1470ea
Packit 1470ea
        # the cellrenderer for the column - text
Packit 1470ea
        renderer_books = Gtk.CellRendererText()
Packit 1470ea
        # the column is created
Packit 1470ea
        column_books = Gtk.TreeViewColumn(
Packit 1470ea
            "Books by Author", renderer_books, text=0)
Packit 1470ea
        # and it is appended to the treeview
Packit 1470ea
        view.append_column(column_books)
Packit 1470ea
Packit 1470ea
        # the books are sortable by author
Packit 1470ea
        column_books.set_sort_column_id(0)
Packit 1470ea
Packit 1470ea
        # add the treeview to the window
Packit 1470ea
        self.add(view)
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)