如何自动滚动 gtk.scrolledwindow?
我在 ScrolledWindow 中有一个 treeview-widget,它在运行时填充。我希望 ScrolledWindow 自动滚动到列表的末尾。每次将一行插入到树视图中时,我通过调整 ScrolledWindow
的 v 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
扩大搜索范围后,我找到了一个与红宝石相关的答案。由于问题与 gtk 相关,因此应该能够用任何语言来解决,如下所示:
您将发生变化的小部件(在我的例子中是树视图)与 gtk.widget 的“size-”连接起来。 allocate' 信号并将 gtk.scrolledwindow 值设置为“upper - 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 thegtk.scrolledwindow
value to "upper - page_size". example:...
link to the original post at ruby-forum.com:
hint hint
fookatchu 的答案可以改进,以便多个小部件可以使用回调:
fookatchu's answer can be improved so that the callback could be used by multiple widgets:
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())
接受的答案帮助我找到了 gtk-rs 中自动滚动到内容结尾问题的 Rust 解决方案。
这是一个可能对其他人有帮助的 Rust 片段:
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:
建议的解决方案都不适合我,但我能够通过这样做获得所需的行为(使用 Go 的 GTk4 绑定):
其他解决方案将移动滚动窗口(我的解决方案包含一个列表框)向下到倒数第二个项目,但不显示最后一个项目。它的行为方式让我认为需要增加上限,因此我尝试通过页面大小增加它,然后将滚动值设置为新的上限。
None of the suggested solutions worked for me, but I was able to get the desired behavior doing this (using GTk4 bindings for Go):
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.
仍然需要回调(我使用了 textView 而不是 treeView):
Still need the callback (I used a textView rather than treeView):