如何处理 Gtk2::Image 对象中的按钮按下操作?
我一直试图在这个 Perl Gtk2 应用程序中获取一个 Gtk2::Image
对象来对按钮按下做出反应,但没有成功。图像按预期显示,但按钮事件未得到处理。我缺少什么?
my $img = Gtk2::Image->new_from_file( $file );
$img->set_property( sensitive => 1 );
$img->can_focus( 1 );
$img->set_events([qw/ button-press-mask button-release-mask /]);
$img->signal_connect(
'button-press-event' => sub {
my ( $self, $event ) = @_;
print STDERR "Coords: ", $event->get_coords;
return;
});
$window->add( $img );
$window->show_all;
I've been trying to get an Gtk2::Image
object in this Perl Gtk2 application to get to react to button presses, but to no avail. The image shows as expected but the button events don't get handled. What am I missing?
my $img = Gtk2::Image->new_from_file( $file );
$img->set_property( sensitive => 1 );
$img->can_focus( 1 );
$img->set_events([qw/ button-press-mask button-release-mask /]);
$img->signal_connect(
'button-press-event' => sub {
my ( $self, $event ) = @_;
print STDERR "Coords: ", $event->get_coords;
return;
});
$window->add( $img );
$window->show_all;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GtkImage 没有任何与之关联的窗口;换句话说,它不会对任何 X 事件做出反应(通常是以
-event
结尾的事件)。处理这些小部件上的事件的常见方法是使用
GtkEventBox
,即将GtkImage
小部件放置在GtkEventBox
内,并将 X 事件信号连接到此GtkEventBox
。GtkImage does not have any window associated to it; in other words it does not react to any X event (generally, the ones ending with
-event
).The common way to handle events on those widgets is by using
GtkEventBox
, that is placing theGtkImage
widget inside aGtkEventBox
and connecting X event signals to thisGtkEventBox
.