[Documentation / Example] Could a specific example for events be added?
Hey there,
Would it be possible to add a specific example for events?
That includes buttons - this is easy.
But it would be nice if more events could be showcased, such as how to respond to mouse-click events in different widgets, including a gtk entry and a gtk label.
And perhaps a few more examples.
This would be helpful for people who port older code from e. g. gtk3 to see how things changed in regards to events. The new event model still confuses me and is one reason I failed to transition into gtk4 so far.
You can see I touch on the subject in this section: https://github.com/Taiko2k/GTK4PythonTutorial#input-handling-in-our-drawing-area
There are different controllers such as EventControllerKey, EventControllerMotion, or GestureClick. You create one and add it to whatever widget using .add_controller(controller).
So for mouse click
controller = Gtk.GestureClick.new()
controller.connect("pressed", self.click) # could be "released"
controller.set_button(0) # 0 for all buttons
self.add_controller(controller) # here self is window but could be any widget
def click(self, gesture, data, x, y):
button = gesture.get_current_button()
print(button)
What I didn't mention which may be useful was if you want to override a widgets normal behavior such as keyboard input on a text entry box, you can do controller.set_propagation_phase(Gtk.PropagationPhase.CAPTURE) then in the callback functions, return True or False as to if you handled the event.