Sorry for the top post - it's just how my reader works.

There are two options here. The first is to pass the number as an argument
to your subroutine. The second is to get the widget reference itself. It all
depends on what information you *really* need and why you need it.

i.e. It makes no sense to have 3 buttons doing the same thing unless you are
passing different information.

Examples - 
##########################
#Method #1:
##########################
use Tk;
use strict;

my $mw=tkinit;
foreach (1..3) {
 $mw->Button(-text=>"Button $_", -command=>[\&display,$_])->pack;
}
MainLoop;

sub display
{
  my ($num) = @_;
  print $num,"\n";
}
##########################
#Method #2:
##########################
use Tk;
use strict;

my $mw=tkinit;
foreach (1..3) {
 $mw->Button(-text=>"Button $_", -command=>\&display)->pack;
}
MainLoop;

sub display
{
print "\$Tk::event = ", $Tk::event,"\n";
print "\$Tk::widget = ", $Tk::widget,"\n";
print "A way to get widget through the event", $Tk::event->W,"\n";
}
##########################

There is a third option where if you bind to say Button-Release, then the
widget reference will be implicitly passed in the argument list.

But method 1 or 2 should work for your needs.

Jack

-----Original Message-----
From: perl-win32-users-boun...@listserv.activestate.com
[mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of
alex.ign...@atcoitek.com
Sent: February-23-09 1:59 PM
To: perl-win32-users@listserv.activestate.com
Subject: A Perl::Tk question


Hi there, I need to know the name of the object that called me from
inside my callback routine.
For instance I have 3 buttons, they all call the same subroutine, if
button number 3 was pressed, I want to be able to determine that at run
time. Even if it's just by reading the button text.  How do I do this? (
small example snippet )
        
my $button1 = $mw->Button(-text => 'button#1',-command => \&display) 
my $button2 = $mw->Button(-text => 'button#2',-command => \&display)
my $button3 = $mw->Button(-text => 'button#3',-command => \&display)



sub display {
        print "I was called by button ???\n"; 
}

This is probably some simple thing that I am not finding at the moment.
I've tried googling this all over the place and can't seem to make any
headway.

Thanks in advance
Alex


The information transmitted is intended only for the addressee and may
contain confidential, proprietary and/or privileged material. Any
unauthorized review, distribution or other use of or the taking of any
action in reliance upon this information is prohibited. If you receive this
in error, please contact the sender and delete or destroy this message and
any copies.
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 8.0.237 / Virus Database: 270.11.3/1966 - Release Date: 02/22/09
17:21:00

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to