Search code examples
pythongtk

gtk python - text view scroll to bottom when text is added?


I am making an application with gtk and python. In the application I have a scroll window and inside of it I have a textview. When I use set_text() in the textbuffer the scroll bar scrolls to the top.

What I want is when I use set_text() is that scroll bar will scroll to the bottom.

Or in other words I want to control the scroll bar to go to the bottom.

How can it be done?

Thanks for the help!


Solution

  • I believe there are 2 ways.

    1. Obtain GtkAdjustments for scrolled window (vertical and horizontal), set it's value to maximum after set_text
    2. A bit more complicated example can be found in gtk3-demo app in Text Widget -> Automatic Scrolling section. There are following lines:

    /* If we want to scroll to the end, including horizontal scrolling, * then we just create a mark with right gravity at the end of the * buffer. It will stay at the end unless explicitly moved with * gtk_text_buffer_move_mark. */ gtk_text_buffer_create_mark (buffer, "end", &iter, FALSE);

    And then:

    /* Get "end" mark. It's located at the end of buffer because
     * of right gravity
     */
    mark = gtk_text_buffer_get_mark (buffer, "end");
    gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
    
    /* and insert some text at its position, the iter will be
     * revalidated after insertion to point to the end of inserted text
     */
    // Extra code removed
    
    /* Now scroll the end mark onscreen.
     */
    gtk_text_view_scroll_mark_onscreen (textview, mark);