手动滚动到 Gtk.ScrolledWindow 中的 child

Manually scroll to a child in a Gtk.ScrolledWindow

我正在开发一个 GTK 应用程序(在 Python 中使用 PyGTK),我需要手动将 Gtk.ScrolledWindow 滚动到它包含的 child 以在屏幕上显示它。

ScrolledWindow包含一个ListBox,里面包含很多ListBoxRow。

self.currencySwitcher = CurrencySwitcher(self) self.currencySwitcherBox = Gtk.ScrolledWindow(vexpand = True) self.currencySwitcherBox.add(self.currencySwitcher)

Gtk.ListBox 的 CurrencySwitcher 继承:

class CurrencySwitcher (Gtk.ListBox): # ...

我有一个搜索系统可以在此 ListBox 中找到特定的行,但是当我通过搜索某行来单击时,它在屏幕上不可见(我必须滚动才能看到它) 所以我必须能够滚动到 child 才能在屏幕上看到它,而不必手动滚动 ScrolledWindow

我该怎么做?我已经搜索了很多,但找不到好的答案。 谢谢

Gtk.ListBox 也继承自 Gtk::Container,因此请查看 Gtk.Containerset_focus_vadjustment()set_focus_hadjustment() 成员。

在您的 ListBox 上调用其中一个或两个函数后,您可以让 ListBoxRow 子项 grab_focus()(继承自 Gtk.Widget),它将滚动到视图中。