Gtk.EventController scope is limited to the block of creation
In gtk 3.24 EventControllers are introduced. These work slightly different to how they are used in gtk4.
GTK 3.24
button = Gtk.Button(label="Click here")
controller = Gtk.EventControllerMotion.new(button)
controller.connect("enter", self.on_button_enter)
The example above (full) does not work, because at the end of the scope the controller is destroyed. Making the controller classwide with self._controller
does work, but feels illogical.
GTK 4
button = Gtk.Button(label="Click Here")
controller = Gtk.EventControllerMotion()
button.add_controller(controller)
controller.connect("enter", self.on_button_enter)
This is how the same thing works in GTK 4 and because the controller is added to the widget, there is no scope problem.
Edited by Christoph Reiter