#!/usr/bin/python

from gi.repository import Gtk, GLib, Gdk

win = Gtk.Window()
win.set_default_size(200, 200)
liststore = Gtk.ListStore(object)
for i in range(100):
    liststore.append(row=[i])

treeview = Gtk.TreeView(model=liststore)

render = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("foo", render)

def cell_data(column, cell, model, iter_, data):
    cell.set_property('text', str(model.get_value(iter_, 0)))

column.set_cell_data_func(render, cell_data)
treeview.append_column(column)

sw = Gtk.ScrolledWindow()
sw.add(treeview)

box = Gtk.VBox()
box.pack_start(sw, True, True, 0)

def button_handler(*args):
    print("scroll to last row")
    length = liststore.iter_n_children(None)
    last_path = Gtk.TreePath.new_from_indices([length - 1])
    treeview.scroll_to_cell(
        path=last_path, column=None,
        use_align=True,row_align=0.5, col_align=0.0)

button = Gtk.Button(label="Scroll to last")
button.connect("clicked", button_handler)
box.pack_start(button, False, True, 0)

win.add(box)

win.connect("delete-event", Gtk.main_quit)
win.show_all()
GLib.unix_signal_add(0, 2, Gtk.main_quit)
Gtk.main()
