如何处理 Gtk2::Image 对象中的按钮按下操作?

发布于 2024-08-25 18:59:30 字数 563 浏览 4 评论 0原文

我一直试图在这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2024-09-01 18:59:30

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 the GtkImage widget inside a GtkEventBox and connecting X event signals to this GtkEventBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文