I’m trying to start cleaning Lollypop code to prepare GTK4 migration but failed to use Gtk.EventControllerScroll to replace a connect on “scroll-event” signal, does not work here.
What is wrong with this code? I tried creating controller on listbox without success.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class View:
def __init__(self):
win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)
scrolled = Gtk.ScrolledWindow()
win.add(scrolled)
listbox = Gtk.ListBox()
scrolled.add(listbox)
for i in range(0, 100):
label = Gtk.Label.new(str(i))
listbox.add(label)
self.controller = Gtk.EventControllerScroll.new(scrolled, Gtk.EventControllerScrollFlags.BOTH_AXES)
self.controller.connect("scroll", self.on_scroll)
win.show_all()
def on_scroll(self, controller, dx, dy):
print(dx, dy)
if __name__ == "__main__":
View()
Gtk.main()
The recommendation to use gestures is mostly meant for your own custom widgets, not for existing GTK widgets.
GtkScrolledWindow is consuming those scroll events already, so your gesture won’t see them. Additionally, GtkScrolledWindow in GTK 3 is not using a scroll gesture.
The question is: what are you trying to achieve? If you want to be notified that the scrolled window is scrolling, you should use the adjustment’s values, instead.