How to auto-scroll a gtk.scrolledwindow?

10,012

Solution 1

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

Solution 2

Python Gtk 3 version:

adj.set_value(adj.get_upper() - adj.get_page_size())

Solution 3

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 )

Solution 4

The accepted answer has helped me figure out a Rust solution in gtk-rs to the auto-scroll to end of content issue.

Here's a Rust snippet that might help others:

// Imports used for reference
use gtk::{TextBuffer, TextView, TextBufferBuilder, ScrolledWindow, ScrolledWindowBuilder};

// The text buffer where the text is appended later
let text_buffer: TextBuffer = TextBufferBuilder::new().build();

// The containing text view that holds the text buffer
let text_view: TextView = TextView::new_with_buffer(&text_buffer);

// The scrolled window container with fixed height/width that holds the text view
let scrolled_window: ScrolledWindow = ScrolledWindowBuilder::new()
        .min_content_height(400)
        .min_content_width(600)
        .child(&text_view)
        .build();

// Have the text view connect to signal "size-allocate"
text_view.connect_size_allocate(clone!(@weak scrolled_window => move |_,_| {
   let adj = scrolled_window.get_vadjustment().unwrap();
   adj.set_value(adj.get_upper() - adj.get_page_size());
}));

// ...
// Later on, fill buffer with some text.
text_buffer.insert(&mut text_buffer.get_end_iter(), "This is my text I'm adding");
Share:
10,012
Fookatchu
Author by

Fookatchu

Updated on June 05, 2022

Comments

  • Fookatchu
    Fookatchu almost 2 years

    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?

  • Fookatchu
    Fookatchu over 10 years
    the member variables seem to be hidden now. use get_upper() and get_page_size() instead
  • jcoppens
    jcoppens almost 7 years
    This does not work in eg. a gtk.treeview: adj's values are updated later, so mostly, the last added row will not appear.
  • Herohtar
    Herohtar over 2 years
    Just a note that this doesn't work with Gtk4 because the size-allocate signal was removed.