简体   繁体   中英

How do I handle button presses in Gtk2::Image objects?

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;

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM