Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching double click event to a label

How can I attach a "clicked" event to a label? I tried GtkEventBox but had no luck with it.

like image 790
aaa Avatar asked Apr 28 '11 16:04

aaa


4 Answers

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

like image 179
ptomato Avatar answered Oct 23 '22 03:10

ptomato


Gtk# differentiates between Widgets and 'Containers'. Most widgets placed on a Gtk# form will NOT receive mouse click events. In order to receive a mouse event you need to place the widget inside a specific container - like EventBox:

  1. Add an EventBox containter to your form. You can place it behind other Widgets or since it is not visible, unless you specifically select it to be (or change its background color).

  2. Put your label widget inside this EventBox. Notice that the label will get the shape and size of the EventBox.

  3. Add to this EventBox the signal 'ButtonPressEvent' out of the "Common Widget Signals".

If you need to identify the button that was clicked while handling this event, use the uint value in: args.Event.Button typically '1' will be the left mouse button, '2' the center button and '3' the right button ('2' might be also when both left and right buttons are clicked).

like image 11
OM55 Avatar answered Oct 23 '22 03:10

OM55


It seems you don't need EventBox at all: Since the problem is that labels don't have any associated X11 window, 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 it should only be used for implementing widgets - but hey, it works. Who cares about "should"?

like image 3
Kyuuhachi Avatar answered Oct 23 '22 05:10

Kyuuhachi


2019-04-17 Update:

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

Original (wrong) answer:

Connect to the button-press-event signal, but directly on the GtkLabel. I'd say you don't need an eventbox here, as GtkLabel already inherits this signal from GtkWidget. To enable the GtkLabel to receive those events, you need first to call gtk_widget_set_events or gtk_widget_add_events, and add the sensitivity to the GDK_BUTTON_PRESS_MASK event.

like image 2
liberforce Avatar answered Oct 23 '22 05:10

liberforce