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

Hi, I'm trying to run a command from a Tk application. It's a command that takes a while (execution of another script) and I don't want the Tk interface to freeze or be busy. I would like to show some sort of 'the process is running animation' (like the moving image in the upper right corner of a web browser while it is loading a page) and the other functions of the gui should still be available to the user. When the external script is done, the animation should stop, so the user knows that the process is finished. The solution should also be portable to a windows system... Can anyone help? Thanks, Martijn

Replies are listed 'Best First'.
Re: background process in a Tk app
by BrowserUk (Patriarch) on Oct 13, 2004 at 12:38 UTC

    This is simple using threads. See Re: Keeping the user informed: A Tk/Threads question for some crude sample code.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
•Re: background process in a Tk app
by merlyn (Sage) on Oct 13, 2004 at 12:10 UTC
Re: background process in a Tk app
by zentara (Cardinal) on Oct 13, 2004 at 14:21 UTC
    There are numerous ways to do what you want, ranging from the nice threads example by BrowserUk, to using "fork-and-exec" in a subroutine. The easiest way is to use the non-blocking Tk::ExecuteCommand module. Here are 2 simple examples, one spawns a new toplevel for the command.
    #!/usr/bin/perl -w use Tk; use Tk::ExecuteCommand; use Tk::widgets qw/LabEntry/; use strict; my $mw = MainWindow->new; my $ec = $mw->ExecuteCommand( -command => '', -entryWidth => 50, -height => 10, -label => '', -text => 'Execute', )->pack; $ec->configure(-command => 'date; sleep 10; date'); my $button = $mw->Button( -text =>'Do_it', -background =>'hotpink', -command => sub{ $ec->execute_command }, )->pack; MainLoop;

    and if you want separate windows, try something like this:

    #!/usr/bin/perl -w use Tk; use Tk::ExecuteCommand; use Tk::widgets qw/LabEntry/; use strict; my $mw = MainWindow->new; my $top = $mw->Toplevel; $top->withdraw; my $ec = $top->ExecuteCommand( -command => '', -entryWidth => 50, -height => 10, -label => '', -text => 'Execute', )->pack; $ec->configure(-command => 'date; sleep 10; date'); my $button = $mw->Button( -text =>'Do_it', -background =>'hotpink', -command => sub{ $top->deiconify; $top->raise; $ec->execute_command; $top->withdraw }, )->pack; MainLoop;

    I'm not really a human, but I play one on earth. flash japh