el-moe has asked for the wisdom of the Perl Monks concerning the following question:

I have a callback assigned to a button:
use Widget_Lib; my $var; my $button = $frame->Button(-text => "merge", -command => sub { $var=&Widget_Lib::show_list; print "$var"; }
The &show_list is another window with a list I need to pick from. The problem is that the print happens before I've even clicked the OK button on the other window... so I'm not getting the value returned. I have checked the value in the other .pm where the list is generated and it is setting my $picked value.

So I guess I'm asking this. Doesn't the program wait until as subroutine returns before continuing on and running the other commands? And if it does... does anyone know why this might be happening?

Thanks for the help

Replies are listed 'Best First'.
RE: pTk popup not returning value and not locking calling window
by Anonymous Monk on Oct 07, 2000 at 23:41 UTC
    I'm not an expert here, but it sounds as if you're not programming your pop-up as a modal window. Check the DialogBox.pm code for clues. It uses the waitVariable method (described in "Learning Perl/Tk", p. 304ff) to wait for the button press. Good luck!
Re: pTk popup not returning value and not locking calling window
by Chmrr (Vicar) on Oct 09, 2000 at 06:07 UTC
    The short answer is that it doesn't wait. The longer reason involves how your function intereacts with the event loop. Your &show_list function sets up and displays the window, probably by making a new TopLevel (we'll call this new TopLevel $dialog). Then, the function returns -- it has no way of knowing that it needs to wait for input; indeed, there is no way to make it stick around inside the subroutine waiting for input. All of the input has to be handled via the event loop. So the only way to do this is to split things up.

    As the poster above said, you need to make $dialog modal. The only time when you're guaranteed to know the value of the variable is when you close $dialog -- so attach all of your computations, etc, to the callback on the "OK" button on $dialog.

    Hope this helps -- if not, ask and I'll post an example of what I mean.

      Here is the OK button from the popup window.
      my $ok_but = $bf->Button( -text => 'Continue', -width => 10, -command => sub { ($idx) = $list_box->curselection(); if ( defined $idx ){ $job = $jobs[$idx]; $mw->destroy(); } else { $mw->destroy(); } )->pack(-side => 'bottom', -padx => 10, -pady => 5);
      . . . at the end of the subroutine i have
      $top->Mainloop(); return $job if defined $job; return 0;
      I must be missing something very obvious or I've been looking at the code too long to see my mistake. Because it always returns 0 before I even click 'Continue'.

      Thanks for the help.
        I'm not a TK guy, but looking at your code, this would occur if $idx wasn't defined. Have you put some debug code at the point to make sure you know which path your're taking on the if (defined $idx) statement?

        --Chris

        e-mail jcwren