在一个窗口中,我有一个Gtk.Frame
,里面有一个图像。现在,我需要在点击Gtk.Frame
时显示一条消息。这是我的代码。
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代码:
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()
但是,当我点击Gtk.Frame
时,没有任何反应。我还尝试对image
中的label
和frame
使用相同的处理程序。仍然无法正常工作。
我需要捕获双击事件,但我知道起点是button-press-event
。
我做错了什么?
答案 0 :(得分:2)
按钮按下事件有两个要求在窗口小部件上工作:
通常不用于输入的小部件没有GdkWindow,因此这不适用于GtkFrame。
我的第一个建议是重新评估需求:用户不希望框架对点击作出反应。你确定要让用户惊喜吗?也许另一种设计会更好用?
如果您的情况确实需要框架可点击,您可以通过添加GtkEventBox作为框架的父级来解决此问题。 GtkEventBox确实有一个GdkWindow,所以你只需要使用widget.add_events()
设置事件掩码并连接到GtkEventBox的按钮事件而不是框架。您也可能希望使用event_box.set_visible_window()
将EventBox设置为不可见,但请阅读该函数的文档以获取详细信息。