Scroll to end of ScrolledWindow/TextView

scrolledwindow tkinter
gtk scrolledwindow example
scrolledwindow wxpython

on a GTK3 program with Python I implemented a log. This is a TextView with TextBuffer in a ScrolledWindow. With a routine I append a new line to this log. Afterwards it should scroll to the last line.

def append_log(self, line):
    self.textbuffer.insert(self.textbuffer.get_end_iter(), "\n"+line, -1)
    # scrolling down

It should look like this: http://www.physik.tu-dresden.de/~s9472632/log_manual.png

But it doesn't work. I've tried the following code.

# nothing happens
adj = self.scrolledwindow.get_vadjustment()
adj.set_value(adj.get_upper()-adj.get_page_size())    # same without subtraction (only upper or page_size)
self.scrolledwindow.set_vadjustment(adj)              # with and without this line

.

# nothing happens
self.textview.scroll_to_iter(self.textbuffer.get_end_iter(), 0.0, False, 0.5, 0.5)

.

# works a bit but scroll bars cover the text (see picture below)
self.textview.scroll_to_mark(self.textbuffer.get_insert(), 0.0, False, 0.5, 0.5)

Picture: http://www.physik.tu-dresden.de/~s9472632/log_scroll.png

It seems the last four arguments of scroll_to* (within_margin, use_align, xalign, yalign) do not have influence of the results.

How get it work?

Bye Markus

So I think I know the problem. I think you've connected the wrong signal so that the TextView hasn't requested a new size yet. The code below uses the size-allocate signal of the TextView and it works like a charm.

#!/usr/bin/env python

from gi.repository import Gtk, GObject


class W(Gtk.Window):

    def __init__(self):

        super(W, self).__init__()
        self.connect("destroy", Gtk.main_quit)
        self.set_default_size(200, 400)

        self._msg_i = 0
        self._sw = Gtk.ScrolledWindow()
        self.add(self._sw)

        self._tw = Gtk.TextView()
        self._tb = self._tw.get_buffer()

        self._sw.add_with_viewport(self._tw)
        #The signal should at least be connected after the scrolled window
        #is created, since the callback works on it.
        self._tw.connect("size-allocate", self._autoscroll)

        GObject.timeout_add(400, self.sim_log)  # Invokes spamming
        self.show_all()

    def sim_log(self, *args):
        """A simple spamming function to emulate some sort of logging"""
        self._msg_i += 1
        i = self._tb.get_end_iter()
        self._tb.insert(i, "\n* Message {0} recieved!".format(self._msg_i), -1)
        return True

    def _autoscroll(self, *args):
        """The actual scrolling method"""
        adj = self._sw.get_vadjustment()
        adj.set_value(adj.get_upper() - adj.get_page_size())


if __name__ == "__main__":

    w = W()
    Gtk.main()

Scrolledwindow: scrolling automatically to the bottom, inside a scrolledwindow; the textview is not editable, and only the scripts adds text to it. when new text is added, i want to scroll to the bottom,  one thing we did in stash, was to add a slight ui.delay after setting text, to scroll to the desired location. I think we determined that content_size might not be immediately updated by the OS, so you have to wait.

I've just had this and made the scroll_to_mark line work. I believe your problem was setting 'use_align' to False. The line should read:

self.textview.scroll_to_mark(self.textbuffer.get_insert(), 0.0, True, 0.5, 0.5)

PyGTK - ScrolledWindow Class, Scrolled window is created to access other widget of area larger than parent window. Some widgets like TreeView and TextView of native support for scrolling​. wx.ScrolledWindow with wx.TextCtrl. Examples of wx.ScrolledWindow with some TextCtrl.. It is necessary to have a wx.ScrolledWindow or a wx. PyScrolledWindow be managed by a sizer to gaurantee that the scrollbars appear when they are needed.

For gtkmm 3.0/C++11:

void append(std::string s) {
  textbuffer->insert(textbuffer->end(), s.c_str());

  // Wait for Gui to Redraw with added line to buffer
  while (gtk_events_pending())
    gtk_main_iteration_do(false);

  // Scoll to end of Buffer
  auto it = textbuffer->get_iter_at_line(textbuffer->get_line_count());
  TextView1.scroll_to(it);
}

gtk.ScrolledWindow, gtk.ScrolledWindow — adds scrollbars to its child widget. TextView , and gtk.​Layout . The position of the scrollbars is controlled by the scroll adjustments. Scroll to the end of a TextView From: Felipe Weckx [ Date Prev ][ Date Next ] [ Thread Prev ][ Thread Next ] [ Thread Index ] [ Date Index ] [ Author Index ]

#!/usr/bin/env python """Text Widget/Automatic scrolling This , ScrolledWindow() hbox.pack_start(swindow) textview = gtk. self.scroll_to_end, textview) else: # If we want to scroll to the bottom, but not scroll horizontally,  The following are code examples for showing how to use gtk.ScrolledWindow().They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Detect manual scrolling in a gtk text view · GitHub, Detect manual scrolling in a gtk text view. README. Q: after inserting text, otherwise you leave the scrolled window position where it is. # EOF #. Q+A.txt. Q:. “overlay-scrolling” gboolean. Whether overlay scrolling is enabled or not. If it is, the scrollbars are only added as traditional widgets when a mouse is present. Otherwise, they are overlayed on top of the content, as narrow indicators. Note that overlay scrolling can also be globally disabled, with the “gtk-overlay-scrolling” setting.

gi.repository.Gtk.ScrolledWindow Python Example, TOPLEVEL, title='') self.scrolls = Gtk.ScrolledWindow() self. to import and then click\n" "the Import button at bottom")) # create self.import_text = Gtk.TextView()  A gtk.ScrolledWindow is a subclass of gtk.Bin that adds scrollbars to a single child widget and optionally draws a beveled frame around the child widget. The scrolled window can work in two ways: The scrolled window can work in two ways:

Comments
  • Make sure you are using the add method for children with native scrolling capabilities. This is container_add in C api.
  • adj.set_value(adj.get_upper() - adj.get_page_size()) just scroll to the first line now showed yet. It would be nice to scroll all down
  • It worked accordingly :)