Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rubyFeedback opened this issue Nov 15, 2022 · 1 comment

Comments

@rubyFeedback
Copy link

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.

@Taiko2k
Copy link
Owner

Taiko2k commented Nov 15, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants