Discussion:
[pygtk] treeview.scroll_to_cell() has no effect
Karl Ostmo
2008-09-13 18:37:36 UTC
Permalink
Does anyone know what is wrong with my usage of scroll_to_cell( ) ? When I
run the program below, after I click on a row and scroll such that it is out
of view, I would expect that when I click on the "Scroll to highlighted row"
button that the row would be brought back into view.

Thanks,
Karl

#!/usr/bin/python

from pygtk import require
require('2.0')
import gtk, gobject

class TreeviewTest(gtk.Window):
def __init__(self):
gtk.Window.__init__( self, gtk.WINDOW_TOPLEVEL )
self.connect("destroy", self.destroy)

vbox = gtk.VBox(False, 5)
self.add( vbox )

button = gtk.Button("Scroll to highlighted row")
button.connect("clicked", self.cb_scroll)
vbox.pack_start(button, False, False)

self.tv = gtk.TreeView( gtk.TreeStore(gobject.TYPE_STRING,
gobject.TYPE_STRING) )

for i, colname in enumerate(["Index", "Name"]):
self.tv.append_column( gtk.TreeViewColumn(colname,
gtk.CellRendererText(), text=i) )

for i in range(20):
self.tv.get_model().append(None, row=(str(i), "Foo"))

scrolled_window = gtk.ScrolledWindow()
scrolled_window.add_with_viewport(self.tv)
vbox.pack_start(scrolled_window, True, True)

self.show_all()

def cb_scroll(self, widget):
selection = self.tv.get_selection()
tm, tree_iter = selection.get_selected()
path = tm.get_path(tree_iter)
print path
self.tv.scroll_to_cell(path)
# gobject.idle_add(self.tv.scroll_to_cell, path) # This doesn't
work either

def destroy(self, widget, data=None):
gtk.main_quit()

if __name__ == "__main__":
application = TreeviewTest()
gtk.main()
Karl Ostmo
2009-01-05 06:20:58 UTC
Permalink
Post by Karl Ostmo
Does anyone know what is wrong with my usage of scroll_to_cell( ) ? When I
run the program below, after I click on a row and scroll such that it is out
of view, I would expect that when I click on the "Scroll to highlighted row"
button that the row would be brought back into view.
Thanks,
Karl
#!/usr/bin/python
from pygtk import require
require('2.0')
import gtk, gobject
gtk.Window.__init__( self, gtk.WINDOW_TOPLEVEL )
self.connect("destroy", self.destroy)
vbox = gtk.VBox(False, 5)
self.add( vbox )
button = gtk.Button("Scroll to highlighted row")
button.connect("clicked", self.cb_scroll)
vbox.pack_start(button, False, False)
self.tv = gtk.TreeView( gtk.TreeStore(gobject.TYPE_STRING,
gobject.TYPE_STRING) )
self.tv.append_column( gtk.TreeViewColumn(colname,
gtk.CellRendererText(), text=i) )
self.tv.get_model().append(None, row=(str(i), "Foo"))
scrolled_window = gtk.ScrolledWindow()
scrolled_window.add_with_viewport(self.tv)
vbox.pack_start(scrolled_window, True, True)
self.show_all()
selection = self.tv.get_selection()
tm, tree_iter = selection.get_selected()
path = tm.get_path(tree_iter)
print path
self.tv.scroll_to_cell(path)
# gobject.idle_add(self.tv.scroll_to_cell, path) # This doesn't
work either
gtk.main_quit()
application = TreeviewTest()
gtk.main()
I ran into this problem again recently and finally solved it. Google turned
up my old post, so I will post the resolution in case it helps someone else.

The error in the above code is my use of the "add_with_viewport" method.
Since a treeview has native scrolling capability, I should have used the
"add" method instead (see
http://www.pygtk.org/docs/pygtk/class-gtkscrolledwindow.html#method-gtkscrolledwindow--add-with-viewport).
Making that change to the code above fixes it!

HTH,
Karl

Loading...