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$..$/

In reply to Re: Perl/TK get the Button's text by liverpole
in thread Perl/TK get the Button's text by Aboveyou

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.