简体   繁体   中英

Scroll to end of ScrolledWindow/TextView

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()

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)

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM