Search code examples
perltk-toolkitperltk

perl tk - how to keep GUI alive when procedure inside perl module is being executed


I have a perl tk routine calling a procedure from an imported perl module. The GUI hangs while the subroutine is being executed. Here is the code excerpt I am using. Can somebody please suggest a better way to do this so that the GUI is active while the procedure is running?

use Tk;
use package1;


$mw = MainWindow->new;
$mw->geometry("+10+10");
$mw->title("My notebook GUI");
my $mwFrame = $mw->Frame(-borderwidth => 2, -relief => 'ridge')->pack(-fill=> 'none',-fil=> 'x');
my $nb = $mwFrame->NoteBook(-dynamicgeometry => 'true',-ipadx => 20, -ipady => 20)->pack(-expand => 1,-fill => 'both');             
$page1 = $nb->add( 'Page1',     -label => 'Page 1' );
$page2 = $nb->add( 'Page2',     -label => 'Page 2' );
    $page1->pack();
    $page2->pack();

    $button1 = $page1->Button(-text => "Not Selected", -background => 'gray', -state => 'disabled')->pack(-side => 'right', -expand => 0);
    $button2 = $page2->Button(-text => "Not Selected", -background => 'gray', -state => 'disabled')->pack(-side => 'right', -expand => 0);

    my $obj = package1->new();
    my $obj->run();

In the above code, I am calling the run procedure from package1 to be executed. While the procedure takes a while, the GUI freezes, not allowing me to do anything on it. I cant even go from one page to the other in the notebook GUI.

Can somebody please guide me? I am a perl newbie.

Thanks.


Solution

  • Welcome to the world of concurrent programming. In general terms, there are three main approaches here:

    1. Threads
    2. Fork / pipe
    3. Cooperative multitasking / event loop

    There are pros and cons to each one.

    Here's one example using the threads approach; I think this could be useful for you.