GTK4PythonTutorial icon indicating copy to clipboard operation
GTK4PythonTutorial copied to clipboard

[Documentation / Example] Could a specific example for events be added?

Open rubyFeedback opened this issue 3 years ago • 1 comments

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.

rubyFeedback avatar Nov 15 '22 04:11 rubyFeedback

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.

Taiko2k avatar Nov 15 '22 06:11 Taiko2k