gtk python - 文本视图在添加文本时滚动到底部?

时间:2018-05-27 18:51:33

标签: python gtk

我正在用gtk和python创建一个应用程序。 在应用程序中,我有一个滚动窗口,在其中我有一个textview。 当我在文本缓冲区中使用set_text()时,滚动条会滚动到顶部。

我想要的是当我使用set_text()时,滚动条将滚动到底部。

或者换句话说我想控制滚动条到底部。

怎么做?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我相信有两种方式。

  1. 获取滚动窗口的GtkAdjustments(垂直和水平),在set_text
  2. 之后将其值设置为最大值
  3. 可以在gtk3-demo部分的Text Widget -> Automatic Scrolling应用中找到更复杂的示例。有以下几行:
  4. /* 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);

    然后:

    /* 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);