in reply to Re: Perl Tk unable to return from MainLoop when implemented in a subroutine
in thread Perl Tk unable to return from MainLoop when implemented in a subroutine

I'm not sure what you want to see, so below is the code for the whole subroutine I'm writing

The whole reason I have this subroutine is for user to select some items in the @newSeqUnique array and return the selected array to the calling function

Currently the code is stuck at the MainLoop; statement

sub getSelectedSeq { my $newSeqUniqueRef = shift; my $grp = shift; my @newSeqUnique = @$newSeqUniqueRef; my @newSeqUniqueSorted = sort{$a->{ID} cmp $b->{ID} || $a->{folder} cmp $b->{folder}} @newSeqUnique; my @displayedArr; my $displayedStr; my @selectedArr; foreach (@newSeqUniqueSorted) { $displayedStr = $_->{ID}." - ".$_->{folder}; push (@displayedArr, $displayedStr); } select STDOUT; foreach (@displayedArr) { print "$_\n"; } # my $mw = MainWindow->new(-title=>"Select sequences"); my $mw = new MainWindow; $mw->Label(-text => "Below is a list of sequences not found in exi +sting group $grp table")->pack(); $mw->Label(-text => "Please select sequences to be added to new ta +ble")->pack(); my $lb = $mw->Scrolled("Listbox", -scrollbars => "osoe", -height =>10, -width => 30, -selectmode => "extended"); my $real_lb = $lb->Subwidget('scrolled'); $real_lb->configure(-borderwidth=>2); $lb->insert("end", @displayedArr); $lb->pack(-fill=>"both"); $mw->Button(-text => "Select", -command => sub{@selectedArr=$lb->curselection;$m +w->destroy();})->pack(); $lb->selectionSet('end'); $lb->see('end'); MainLoop(); foreach (@selectedArr) { print "$_\n"; } my @arr; my @temp; my ($index, $ID, $folder, $obj); if ((@selectedArr)&&(defined $selectedArr[0])) { foreach (@selectedArr) { push (@arr, $newSeqUniqueSorted[$_]); } }else { @arr = (); } return \@arr; }

Replies are listed 'Best First'.
Re^3: Perl Tk unable to return from MainLoop when implemented in a subroutine
by choroba (Cardinal) on Mar 23, 2016 at 10:56 UTC
    I can't replicate your problem. I added use Tk; to the top of the script, and the following to the bottom:
    print %{ getSelectedSeq([{ ID => 1, folder => '/' }, { ID => 2, folder => 'home' }], 12)->[0] };

    I'm getting the output and the program terminates.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      I have spent more than 1 day on this bug and I have completely no idea why I have this problem. In my program there are also other subroutines which display popup windows but none of them have this problem. I notice that to display other pop-ups I used $main->Show;, probably that's why I never encountered this problem?

      Is there any other way of displaying this selection box without having to write MainLoop; at all? So far I have tried 2 ways as shown in the code but they all hang. I'm using Perl 5.8.4 and I can't install any additional library.

        Try using Tk->exit

      When I try Tk::exit;, the whole program terminates, not just the GUI :(((

        Ok, stick with dialogs in that case :D

      Can you enlighten me on how I can use dialog box to do the same purpose? :P

      I wanted to use dialog to do this, but could not find any code example on how :(. If can do this with dialog box it would be great!

        #!/usr/bin/perl -- use strict; use warnings; use Tk; my $omw = tkinit(); $omw->withdraw; ## no show my $theword ; my $mw = $omw->Dialog( -textvariable => \$theword ); $mw->transient(undef); ## let dialog show even if MainWindow withdrawn for my $ix ( 1..3 ){ $theword = "bird " x $ix; print join ' ', scalar(gmtime ), $mw->Show, ## "MainLoop" "\n"; sleep 1; }