in reply to Perl::Tk - Event sequencing question

Typically for a long procedural sequence in Tk, I farm the procedural step out to a subroutine, usually with a go button. The subroutine can then loop through all the steps I want done without the need to return to the GUI event model. The GUI can be updated periodically from the subroutine (pass mainwindow in to the subroutine as a parameter) in a couple of different ways.

If shelling out to command line, I often just capture the output with qx or backticks and post that capture back to the mainwindow. This has the drawback that the GUI will not be updated until the command completes. This hasn't been a problem for me since most of my GUI apps also involve counters of some kind. Updating those prevents the user from wondering if the screen has frozen.

If you want to pipe the output of tar to a textbox, you could map STDIN to the text box. I haven't done this yet, just read about it (Mastering Perl/Tk I think) but I keep looking for a chance to use it.

  • Comment on Re: Perl::Tk - Event sequencing question

Replies are listed 'Best First'.
Re: Re: Perl::Tk - Event sequencing question
by crabbdean (Pilgrim) on Mar 25, 2004 at 00:33 UTC
      Yeah, I've had that problem with long running processes too. I once tried to implement an archive process one file at a time with a counter updating the main screen but it was an ugly, inefficient thing. I went with a command line instead.

      Thanks for the link. It answered several questions I've had kicking around in the back of my mind. Maybe I'll be more motivated to try a solution sooner rather than later.

      PJ
        You know after some thought and investigation it became apparant to me that if you have a long running process you always need to output the "fileevent" using the below subroutine. If not your gui freezes until the event finishes. Personally I think this subroutine should included as a feature or at least in the code given in the "fileevent" documentation considering it appears to be a common thing people want to do.

        Essentially it goes like this ... create a widget to write to (a scrollable text box in your mainwindow $mw in this example), open your handle and call the fileevent ...
        my $tx = $mw->Scrolled("Text", -width => 80, -height => 25, -wrap => 'none', )->pack(-expand => 1, -fill => 'both'); open(CHILD, "ls -laR |") or die "Can't open: $!"; CHILD->autoflush(1); $mw->fileevent('CHILD', 'readable', [\&output, \*CHILD, $mw, $tx]);
        Include the following subroutine to output what is streamed to the filehandle without freezing the GUI.
        sub output { my ($handle, $widget, $tx) = @_; if (sysread ($handle, $_, 128)) { $tx->insert('end', $_); # Append the data read $tx->yview('end'); } else { $tx->insert('end', "\nALL DONE\n"); $tx->yview('end'); $widget->fileevent($handle, "readable", undef); # cancel bindi +ng ###----->>> Add in cancel routine here if you want to exit gra +cefully return; } $widget->idletasks; }

        Dean
        The Funkster of Mirth
        Programming these days takes more than a lone avenger with a compiler. - sam
        RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers