[Wellington-pm] GTK2 - labels on buttons
Grant McLean
grant at mclean.net.nz
Tue Mar 14 12:37:37 PST 2006
On Wed, 2006-03-15 at 08:51 +1300, Cliff Pratt wrote:
> Researching some GTK2 stuff I came across an article by one Grant McLean
> about GTK2-Perl/GladeXML:
>
> http://live.gnome.org/GTK2-Perl/GladeXML/Tutorial
>
> Well done Grant and thanks!
You're welcome.
> But, there's always a but, I want to change the labels on an existing
> GTK2 button. Any ideas on that one, anyone?
The documentation for all the widgets is here:
http://gtk2-perl.sourceforge.net/doc/pod/
For the Gtk2::Button widget specifically, it's here:
http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/Button.html
The set_label method is the one you're after. You can either pass it a
plain string, or a widget object (typically a label) that you want
displayed on the button.
> Say it says "Click Here". I want it to basically toggle to say something
> like "Clicked" (and possibly change the style.....)
You might want to look at using a Gtk2::ToggleButton instead, when
pressed, it stays in until pressed again.
http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/ToggleButton.html
Changing style is a little more complicated. Pango is a markup language
(a bit like HTML) for styling text on Gtk labels. To use Pango you need
to call set_markup rather than set_label and you need to call the method
on the label object contained in the button rather than on the button
itself. You can get the label object from a button using the get_child
method (which Gtk2::Button inherits from Gtk2::Bin).
That was a very dense paragraph, so an example might help clarify
things:
#!/usr/bin/perl
use strict;
use warnings;
use Gtk2 -init;
use Glib qw(TRUE FALSE);
my($count1, $count2) = (0, 0);
my $app_win = Gtk2::Window->new;
$app_win->signal_connect(destroy => sub { Gtk2->main_quit; });
my $table = Gtk2::Table->new(2, 2, TRUE);
my $button1 = Gtk2::Button->new_with_label('Left');
$button1->signal_connect(clicked => \&button1_on_click);
$table->attach($button1, 0, 1, 0, 1, ['expand', 'fill'], ['fill'], 4,
2);
my $button2 = Gtk2::Button->new_with_label('Right');
$button2->signal_connect(clicked => \&button2_on_click);
$table->attach($button2, 1, 2, 0, 1, ['expand', 'fill'], ['fill'], 4,
2);
my $label = Gtk2::Label->new('Click a button');
$table->attach($label, 0, 2, 1, 2, ['expand', 'fill'], ['fill'], 4, 2);
$app_win->add($table);
$app_win->show_all;
Gtk2->main;
exit;
sub button1_on_click {
my($button) = @_;
$label->set_label("Left button clicked");
$button->set_label(sprintf("Left (%d)", ++$count1));
}
sub button2_on_click {
my($button) = @_;
$label->set_label("Right button clicked");
my $button_label = $button->get_child;
if($count2 % 2) { # Odd or even?
$button_label->set_markup(
sprintf("<b><i>Right (%d)</i></b>", ++$count2)
);
}
else {
$button_label->set_markup(
sprintf(
'<span foreground="blue" background="#ffff99">Right' .
' (%d)</span>', ++$count2
)
);
}
}
Cheers
Grant
More information about the Wellington-pm
mailing list