如何自动滚动 gtk.scrolledwindow?

发布于 2024-10-20 22:04:33 字数 535 浏览 1 评论 0原文

我在 ScrolledWindow 中有一个 treeview-widget,它在运行时填充。我希望 ScrolledWindow 自动滚动到列表的末尾。每次将一行插入到树视图中时,我通过调整 ScrolledWindowv adjustment 来“解决”这个问题。例如:

if new_line_in_row:
   adj = self.scrolled_window.get_vadjustment()
   adj.set_value( adj.upper - adj.page_size )

如果我在交互式 ipython 会话中运行代码并自行设置值,则一切都会按预期进行。

如果我使用默认的 python 解释器运行代码,自动滚动不会一直工作。我调试了代码,问题似乎是,调整值有某种“滞后”,并且仅在一段时间后才会更改。

我的问题是:如何可靠地滚动到 ScrolledWindow 的最大位置?是生成一个我可以使用的特殊信号吗?或者有更好的方法来设置调整值

I have a treeview-widget inside a ScrolledWindow, which is populated during runtime. I want the ScrolledWindow to auto-scroll to the end of the list. I "solved" the problem, by adjusting the vadjustment of the ScrolledWindow, everytime a row is inserted into the treeview. e.g:

if new_line_in_row:
   adj = self.scrolled_window.get_vadjustment()
   adj.set_value( adj.upper - adj.page_size )

If i run the code in an interactive ipython session and set the value by myself, everything works as expected.

If i run the code with the default python interpreter, the auto-scroll doesn't work all the time. I debugged the code and the problem seems be, that the adjustment values have some kind of "lag" and are only changed after some period of time.

My question is: how do I scroll, reliably, to maximum position of the ScrolledWindow? is a special signal generated which i can use? or is there a better way to set the adjustment-value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

悲欢浪云 2024-10-27 22:04:33

扩大搜索范围后,我找到了一个与红宝石相关的答案。由于问题与 gtk 相关,因此应该能够用任何语言来解决,如下所示:

您将发生变化的小部件(在我的例子中是树视图)与 gtk.widget 的“size-”连接起来。 allocate' 信号并将 gtk.scrolledwindow 值设置为“upper - page_size”。示例:

self.treeview.connect('size-allocate', self.treeview_changed)

...

def treeview_changed(self, widget, event, data=None):
    adj = self.scrolled_window.get_vadjustment()
    adj.set_value( adj.upper - adj.page_size )

链接到 ruby​​-forum.com 上的原始帖子:

提示提示

After widening my search-radius, i found a ruby-related answer. since the problem is gtk-related, it should be able to be solved in any language like this:

you connect the widget which changes, in my case the treeview, with gtk.widget's 'size-allocate' signal and set the gtk.scrolledwindow value to "upper - page_size". example:

self.treeview.connect('size-allocate', self.treeview_changed)

...

def treeview_changed(self, widget, event, data=None):
    adj = self.scrolled_window.get_vadjustment()
    adj.set_value( adj.upper - adj.page_size )

link to the original post at ruby-forum.com:

hint hint

早乙女 2024-10-27 22:04:33

fookatchu 的答案可以改进,以便多个小部件可以使用回调:

def treeview_changed( self, widget, event, data=None ):
    adj = widget.get_vadjustment()
    adj.set_value( adj.upper - adj.pagesize )

fookatchu's answer can be improved so that the callback could be used by multiple widgets:

def treeview_changed( self, widget, event, data=None ):
    adj = widget.get_vadjustment()
    adj.set_value( adj.upper - adj.pagesize )
时光清浅 2024-10-27 22:04:33

Python Gtk 3 版本:

adj.set_value(adj.get_upper() - adj.get_page_size())

Python Gtk 3 version:

adj.set_value(adj.get_upper() - adj.get_page_size())

吲‖鸣 2024-10-27 22:04:33

接受的答案帮助我找到了 gtk-rs 中自动滚动到内容结尾问题的 Rust 解决方案。

这是一个可能对其他人有帮助的 Rust 片段:

// Imports used for reference
use gtk::{TextBuffer, TextView, TextBufferBuilder, ScrolledWindow, ScrolledWindowBuilder};

// The text buffer where the text is appended later
let text_buffer: TextBuffer = TextBufferBuilder::new().build();

// The containing text view that holds the text buffer
let text_view: TextView = TextView::new_with_buffer(&text_buffer);

// The scrolled window container with fixed height/width that holds the text view
let scrolled_window: ScrolledWindow = ScrolledWindowBuilder::new()
        .min_content_height(400)
        .min_content_width(600)
        .child(&text_view)
        .build();

// Have the text view connect to signal "size-allocate"
text_view.connect_size_allocate(clone!(@weak scrolled_window => move |_,_| {
   let adj = scrolled_window.get_vadjustment().unwrap();
   adj.set_value(adj.get_upper() - adj.get_page_size());
}));

// ...
// Later on, fill buffer with some text.
text_buffer.insert(&mut text_buffer.get_end_iter(), "This is my text I'm adding");

The accepted answer has helped me figure out a Rust solution in gtk-rs to the auto-scroll to end of content issue.

Here's a Rust snippet that might help others:

// Imports used for reference
use gtk::{TextBuffer, TextView, TextBufferBuilder, ScrolledWindow, ScrolledWindowBuilder};

// The text buffer where the text is appended later
let text_buffer: TextBuffer = TextBufferBuilder::new().build();

// The containing text view that holds the text buffer
let text_view: TextView = TextView::new_with_buffer(&text_buffer);

// The scrolled window container with fixed height/width that holds the text view
let scrolled_window: ScrolledWindow = ScrolledWindowBuilder::new()
        .min_content_height(400)
        .min_content_width(600)
        .child(&text_view)
        .build();

// Have the text view connect to signal "size-allocate"
text_view.connect_size_allocate(clone!(@weak scrolled_window => move |_,_| {
   let adj = scrolled_window.get_vadjustment().unwrap();
   adj.set_value(adj.get_upper() - adj.get_page_size());
}));

// ...
// Later on, fill buffer with some text.
text_buffer.insert(&mut text_buffer.get_end_iter(), "This is my text I'm adding");
笑着哭最痛 2024-10-27 22:04:33

建议的解决方案都不适合我,但我能够通过这样做获得所需的行为(使用 Go 的 GTk4 绑定):

adj := scrolledWindow.VAdjustment()
adj.SetUpper(adj.Upper() + adj.PageSize())
adj.SetValue(adj.Upper())

其他解决方案将移动滚动窗口(我的解决方案包含一个列表框)向下到倒数第二个项目,但不显示最后一个项目。它的行为方式让我认为需要增加上限,因此我尝试通过页面大小增加它,然后将滚动值设置为新的上限。

None of the suggested solutions worked for me, but I was able to get the desired behavior doing this (using GTk4 bindings for Go):

adj := scrolledWindow.VAdjustment()
adj.SetUpper(adj.Upper() + adj.PageSize())
adj.SetValue(adj.Upper())

The other solutions would move the scrolled window (mine contained a list box) down to the 2nd-to-last item, but not show the last item. The way it was acting made me think that the upper limit needed to be increased, so I tried increasing it by page-size then setting the scroll value to the new upper limit.

往事风中埋 2024-10-27 22:04:33

仍然需要回调(我使用了 textView 而不是 treeView):

textView = Gtk.TextView()
scrolledWindow = Gtk.ScrolledWindow()

def textViewChanged( self, widget ):
    adjustment = scrolledWindow.get_vadjustment()
    adjustment.set_value( adjustment.get_upper() - adjustment.get_page_size() )

textView.connect( "size-allocate", textViewChanged )

Still need the callback (I used a textView rather than treeView):

textView = Gtk.TextView()
scrolledWindow = Gtk.ScrolledWindow()

def textViewChanged( self, widget ):
    adjustment = scrolledWindow.get_vadjustment()
    adjustment.set_value( adjustment.get_upper() - adjustment.get_page_size() )

textView.connect( "size-allocate", textViewChanged )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文