Connecting Signals with Perl and GtkBuilder
-
27-10-2019 - |
Question
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
{
}
Solution
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.)
OTHER TIPS
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");
}