Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-scroll a gtk.scrolledwindow?

Tags:

I have a treeview-widget inside a ScrolledWindow, which is populated during runtime. I want the ScrolledWindow to auto-scroll to the end of the list. I "solved" the problem, by adjusting the vadjustment of the ScrolledWindow, everytime a row is inserted into the treeview. e.g:

if new_line_in_row:    adj = self.scrolled_window.get_vadjustment()    adj.set_value( adj.upper - adj.page_size ) 

If i run the code in an interactive ipython session and set the value by myself, everything works as expected.

If i run the code with the default python interpreter, the auto-scroll doesn't work all the time. I debugged the code and the problem seems be, that the adjustment values have some kind of "lag" and are only changed after some period of time.

My question is: how do I scroll, reliably, to maximum position of the ScrolledWindow? is a special signal generated which i can use? or is there a better way to set the adjustment-value?

like image 658
Fookatchu Avatar asked Mar 07 '11 11:03

Fookatchu


2 Answers

After widening my search-radius, i found a ruby-related answer. since the problem is gtk-related, it should be able to be solved in any language like this:

you connect the widget which changes, in my case the treeview, with gtk.widget's 'size-allocate' signal and set the gtk.scrolledwindow value to "upper - page_size". example:

self.treeview.connect('size-allocate', self.treeview_changed) 

...

def treeview_changed(self, widget, event, data=None):     adj = self.scrolled_window.get_vadjustment()     adj.set_value( adj.upper - adj.page_size ) 

link to the original post at ruby-forum.com:

hint hint

like image 189
Fookatchu Avatar answered Sep 29 '22 06:09

Fookatchu


fookatchu's answer can be improved so that the callback could be used by multiple widgets:

def treeview_changed( self, widget, event, data=None ):     adj = widget.get_vadjustment()     adj.set_value( adj.upper - adj.pagesize ) 
like image 42
Chuck R Avatar answered Sep 29 '22 06:09

Chuck R