Reputation: 1254
In a window I have a Gtk.Frame
with an image inside it. Now I need show a message when the Gtk.Frame
is clicked. Here is my code.
Glade XML:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkFrame" id="frame1">
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Clique para imprimir</property>
<property name="halign">start</property>
<property name="valign">start</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<signal name="button-press-event" handler="onFrameClicked" swapped="no"/>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<signal name="button-press-event" handler="onFrameClicked" swapped="no"/>
<child>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-home</property>
<signal name="button-press-event" handler="onFrameClicked" swapped="no"/>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">frame1</property>
<signal name="button-press-event" handler="onFrameClicked" swapped="no"/>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
Python code:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
def on_frame_clicked(widget, event):
print('Clicked!')
builder = Gtk.Builder.new()
builder.add_from_file('frame_event.glade')
window = builder.get_object('window1')
handlers = {'onFrameClicked': on_frame_clicked}
builder.connect_signals(handlers)
window.show_all()
Gtk.main()
But, when I click on Gtk.Frame
nothing happens. I also tried using the same handler for the image
and the label
in the frame
. Still does not work.
I need capture the double click event, but I know that the start point is the button-press-event
.
What am I doing wrong?
Upvotes: 0
Views: 1337
Reputation: 14587
There's two requirements for button-press-event to work on a widget:
Widgets that are not normally used for input do not have a GdkWindow so this won't work with GtkFrame.
My first suggestion is to re-evaluate the need: Users do not expect frames to react to clicking. are you sure you want to surprise your users? Maybe another design would work better?
If your situation really does require the frame to be clickable, you can solve this by adding a GtkEventBox as the parent of the frame. The GtkEventBox does have a GdkWindow so you only need to use widget.add_events()
to set the event mask and connect to the button-press-event of the GtkEventBox instead of the frame. You also probably want to set the EventBox invisible with event_box.set_visible_window()
but please read the documentation on that function for details.
Upvotes: 2