Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Perl/TK get the Button's text

by liverpole (Monsignor)
on Jun 03, 2009 at 18:17 UTC ( [id://768104]=note: print w/replies, xml ) Need Help??


in reply to Perl/TK get the Button's text

Hi Aboveyou,

(Not to be confused with "high aboveyou" :-))

If you want to do this dynamically, so that it will work even after the button's text has changed, you can either use the -textvar option suggested above, or you can construct the button in two steps.

For example, the first step would be something like:

my $button = $mw->Button(-text => $label);
and in the second step, a subroutine (which takes the button name as an argument) is configured as the command invoked when the button is pressed:

$button->configure(-command => [ \&show_button_label, $button ]); -or- $button->configure(-command => sub { show_button_label($button) }) +;

Obviously you can't do this in one step, as $button isn't defined until you have the return value from $mw->Button, and you need to pass $button to the subroutine.

Here's a full example:

#!/usr/bin/perl # Libraries use strict; use warnings; use Tk; # User-defined my @button_labels = (qw( Red Blue Green White Yellow Orange )); # Main program my $mw = new MainWindow(); foreach my $label (@button_labels) { # Note labels are colors, so they can be used for the background my $button = $mw->Button(-text => $label, -bg => $label); $button->configure(-command => [ \&show_button_label, $button ]); $button->pack(-side => "left"); } MainLoop(); # Subroutines sub show_button_label { my ($button) = @_; my $label = $button->cget(-text); print "Label for button '$button' is '$label'\n"; }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://768104]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-23 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found