4

I'm trying to figure how to access a widget from within a signal handler.

I've got a label called "lblVerify" that I just want to change the text to "verified" when I click on the button. I know I need to use something like Gtk2::Label->set_text but I'm not entirely sure how to access the widget properties from within the on_btnVerify_clicked function.

 #!/usr/bin/perl

use strict;
use warnings;
use Glib qw{ TRUE FALSE };
use Gtk2 '-init';

my $builder;
my $window;

# get a new builder object
$builder = Gtk2::Builder->new();

# load the Gtk File from GLADE
$builder->add_from_file( "testglade.xml" )
    or die "Error loading GLADE file";

# create the main window
$window = $builder->get_object( "window1" )
    or die "Error while creating Main Window";

# connect the event handlers    
$builder->connect_signals( undef );


$window->show_all();


$builder = undef;

Gtk2->main();

exit;



sub on_btnVerify_clicked
{



}   
Chad P
  • 143
  • 5

2 Answers2

2

You need to pass the widgets you want to access as a "user data" parameter to the signal handler. In this case, you would do something like

$label = $builder->get_object("lblVerify");
$builder->connect_signals($label);

which passes the label as the user data parameter to all your signal handlers. Then the arguments passed to on_btnVerify_clicked will be the button itself and the label. (Sorry for any errors, my Perl is quite rusty.)

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • This worked for the first function/widget, but I'm not entirely sure how to pass more widgets and access them in other signal handlers. I've tried a few things but come up short. Any suggestions? – Chad P May 02 '11 at 18:07
  • Yes, what usually happens in a larger application is that you create a structure (or in Perl, probably a hash) of all the widget pointers that you will need to access in signal handlers. Then you pass that hash as the user data parameter. – ptomato May 03 '11 at 09:42
0

Thanks ptomato.

That was what I needed to get the widget passed to the function. As for the function itself, this worked:

sub on_btnSpacewalkVerify_clicked
{

    my $self = shift;
    my $label = shift;

    $label->set_text("Verified");

}   
Chad P
  • 143
  • 5