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

Howdy, In the snippet of code below, I want the user to enter information in the entry widget, and only after pressing return, display the dialog. More specifically, I want the code to wait at $entry->get and process the conditional only after a response is entered. The current code just hangs. I can type in the entry widget but the program never advances. The entry widget is embedded in a main window. Thank you for any suggestions. my $name = ""; $entry->waitVariable(\$name); $name = $entry->get; if ($name ne "" ) { print OUT "$name\n"; $entry->delete(0,length($name)); $entry->update; } $main->Dialog(-title => "Begin Task", -text => "Click OK to begin first task")->Show; usleep(2500000);

Replies are listed 'Best First'.
Re: How to use waitVariable
by Marshall (Canon) on Mar 12, 2009 at 03:46 UTC
    Hi, when you post code, you can enclose the words code and /code in angle<> brackets. This helps with the formatting!

    Update: Oh, I see,usleep(2500000); this is a problem!, you don't want to sleep! If you do that, the stuff that runs the GUI is not gonna be running. Once you start the GUI, it waits for some event to happen.
    original post continues, may still be of value to you...

    Here is a snippet from one of my programs. The text variable is where the stuff goes that is typed in (there is a search command that the user types in here). Then I bind the RETURN key to an anon sub that just calls another routine. I haven't used this waitVariable thing and I'm not sure what it would different than this. Our functional requirement sounds similar so...here's some code..

    $f_dialog->Label (-text => 'Search Query')->pack(-side=>'left'); my $e_search =$f_dialog->Entry (-textvariable => \$tv_search_command,) +->pack(-side=>'left'); $e_search->bind("<Return>", sub {enter_pressed();} ); ...... sub enter_pressed() { #first few lines of this sub... $text->delete("1.0", 'end'); #Update 000ops this is for another wind +ow #not needed for entry window #I think text visible to user entry gets #cleared by the time we are here. #I do think making a copy of entered text for #further processing is good idea though... my $search_command = $tv_search_command; ....blah }
    I think you can just give the address of "enter_pressed()" rather they way this was done. This anon sub may have done more than just call one routine in the past, its been a few years since I've looked at this code. Of course you may not need a separate sub and can do it all in the anon sub. Adapt as you need.

    If you want a button instead of using Return, then you don't need the bind. Just associate a routine with a click button.

      Thanks so much for the alternative suggestion. I understand what to do.
Re: How to use waitVariable
by zentara (Cardinal) on Mar 12, 2009 at 12:55 UTC
    here is another basic waitVariable example:
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new( -title=>"Test"); $mw->geometry('200x200'); $mw->withdraw; my $lab = $mw->Label(-text => "Wait while loading data")->pack(); $mw->Popup; $mw->Busy( -recurse => 1 ); my $data = 0; &init_data(); $mw->waitVariable(\$data); #will wait until data is set to 1 $mw->Unbusy( -recurse => 1 ); $lab->packForget; my $lab1 = $mw->Label(-text => "Ready to go")->pack(); MainLoop; sub init_data{ #simulates getting data $mw->after(4000,sub{ $data = 1; }); }

    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness