【问题标题】:gtk python - text view scroll to bottom when text is added?gtk python - 添加文本时文本视图滚动到底部?
【发布时间】:2021-12-29 03:04:32
【问题描述】:

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

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

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

怎么做?

感谢您的帮助!

【问题讨论】:

    标签: python gtk


    【解决方案1】:

    我相信有两种方法。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      相关资源
      最近更新 更多