Re: (fwd) events

Steve Lidie <[email protected]>
Newsgroups gmane.comp.lang.perl.tk
Message-ID <[email protected]>
>
> I have a perltk program with several widgets that display data in 
> different ways. I want to send a "<<record_added>>"
> event and have anything that should do something ... do it. But it 
> seems that only the last widget bound to the event
> will find out about the event. The code below shows an example of this:
>
> use Tk;
> my $app = new MainWindow;
> my $entry1  = $app->Entry()->pack();
> my $entry2  = $app->Entry()->pack();
>
> $entry1->bind('all', '<<customEvent>>' => sub{ print "entry 1" });
> $entry2->bind('all', '<<customEvent>>' => sub{ print "entry 2" });
>
> $app->Button(-text => 'gen event',
>               -command => sub{ $app->eventGenerate('<<customEvent>>')}
> +)->pack();
>
> MainLoop;
>
>
> Do I just have the wrong mindset or am I doing something wrong in the 
> code? I would think that once an event was fired
> off every widget would get a chance at responding to it; at least 
> every widget you asked to listen for it.
>

You can't use events in a broadcast fashion, they are destined for a 
particular widget.  You can keep a list of the "do something" wigets 
like this:

#!/usr/local/bin/perl -w
use Tk;
use strict;

our @frog;

my $app = new MainWindow;
my $mw = $app;

push @frog, $mw->Entry()->pack() foreach ( 1 .. 4 );

$mw->Button(
	    -text    => 'gen event',
	    -command => \&frog,
)->pack();

MainLoop;

sub frog {
     print "widget=$_!\n" foreach ( @frog );
}

Oo perhaps -textvariables might be useful ... but I/we need to know 
what you really have in mind to come up with a good solution ...

-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.