Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

2 widgets respond to same event

by blueberryCoffee (Scribe)
on Apr 20, 2005 at 19:03 UTC ( #449727=perlquestion: print w/replies, xml ) Need Help??

blueberryCoffee has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

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.

Thanks,

Replies are listed 'Best First'.
Re: 2 widgets respond to same event
by eibwen (Friar) on Apr 21, 2005 at 01:46 UTC
    From the CPAN documentation:
    If more than one binding matches a particular event and they have the same tag, then the most specific binding is chosen and its callback is evaluated. The following tests are applied, in order, to determine which of several matching sequences is more specific:
    1. an event pattern that specifies a specific button or key is more specific than one that doesn't;
    2. a longer sequence (in terms of number of events matched) is more specific than a shorter sequence;
    3. if the modifiers specified in one pattern are a subset of the modifiers in another pattern, then the pattern with more modifiers is more specific.
    4. a virtual event whose physical pattern matches the sequence is less specific than the same physical pattern that is not associated with a virtual event.
    5. given a sequence that matches two or more virtual events, one of the virtual events will be chosen, but the order is undefined.

    If the matching sequences contain more than one event, then tests (c)-(e) are applied in order from the most recent event to the least recent event in the sequences. If these tests fail to determine a winner, then the most recently registered sequence is the winner.

    If there are two (or more) virtual events that are both triggered by the same sequence, and both of those virtual events are bound to the same window tag, then only one of the virtual events will be triggered, and it will be picked at random:

      So it sounds like it's not possible in the way I had hoped. I guess a workaround might be to have a MyEvents module that responds to the event and then hardcode the calls to the widgets that need to respond (or have the widgets register themselves with the module and calls everything in it's list).

      Thanks,
Re: 2 widgets respond to same event
by starbolin (Hermit) on Apr 21, 2005 at 01:23 UTC

    Your events have to bind to different tags. See: TK::bind Search down to "Multiple Matches".


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: 2 widgets respond to same event
by japhy (Canon) on Apr 21, 2005 at 00:09 UTC
    I think the event handlers (your code references) need to return false in order for the underlying widget manager to allow another widget to handle the event. This is only a guess.
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2023-01-27 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?