Attaching a double-click event to a shortcut

How can I tag an event with a click? I tried GtkEventBox but I had no luck.

+6
source share
4 answers

Connect to the button-press-event signal in the EventBox.

+10
source

Gtk # distinguishes Widgets and Containers. Most widgets hosted on the Gtk # form will NOT receive mouse click events. To receive a mouse event, you need to place the widget inside a specific container - for example, EventBox:

  • Add an EventBox formula to the form. You can place it behind other widgets or, since it does not appear, unless you specifically select it (or change its background color).

  • Place your tag widget inside this EventBox. Note that the label will receive the shape and size of the EventBox.

  • Add to this EventBox the โ€œButtonPressEventโ€ signal from the โ€œCommon Widget Signalsโ€.

If you need to identify the button that was pressed during the processing of this event, use the uint value in: args.Event.Button usually โ€œ1โ€ will be the left mouse button, โ€œ2โ€ will be the center button and โ€œ3โ€, the right button (โ€œ2โ€ can also be by pressing the left and right buttons).

+7
source

2019-04-17 Update:

ptomato is right here, GtkLabel is one of the exceptions that really requires an event window, so you must connect to the button-press-event signal of button-press-event window. For other widgets, the install / add event APIs in my original answer should still be relevant.

Original (wrong) answer:

Connect to the button click event signal, but directly to GtkLabel. I would say that you do not need a set of events here, since GtkLabel already inherits this signal from GtkWidget. To activate GtkLabel to receive these events, you must first call gtk_widget_set_events or gtk_widget_add_events and add sensitivity to the GDK_BUTTON_PRESS_MASK event.

+1
source

You don't seem to need an EventBox at all: since the problem is that the labels do not have any X11 window connected, just give it one with set_has_window(True) ! The following works for me:

  self.label = Gtk.Label() self.label.set_has_window(True) self.label.set_events(Gdk.EventMask.BUTTON_PRESS_MASK) self.label.connect("button-press-event", self.click_label) 

The documentation says that it should only be used to implement widgets - but hey, it works. Who cares about the "must"?

0
source

Source: https://habr.com/ru/post/886900/


All Articles