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

Hello monks ^^ I'm wondering how you call another .pl file from a command of button.

some pseudo code indicating more or less what i mean is:

my $button = $mw->Button(-text => "Open another .pl file which contains the code of another window",-command => sub { gui2.pl })->pack();

(i assume that if the file is not in the same folder then i need to specify a path as well.)

I hope my question is clear, I don't know if i'm just missing something but I can't seem to figure out how to do it could you give me a hand?

thanks a lot :)

Replies are listed 'Best First'.
Re: Open another Tk window
by zentara (Cardinal) on Feb 24, 2005 at 16:41 UTC
    When you say, "call another program" you need to be very careful when running a gui like Tk. Why? Because you will run into the problem of "blocking the gui". If you call "system some_other_perl_script", the tk gui will become unresponsive, while the cpu services the call, and runs the other script. So what you want to do is "fork and exec" and return the results in another window.

    There is a very good module for doing this, called Tk::ExecuteCommand. Here is a basic example. You can do alot with this module, like pack it into different toplevel windows, instead of the mainwindow. A second example below does that. You can also change the appearance of the widget, by accessing it's "Advertised Subwidgets".

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::ExecuteCommand; my $mw = MainWindow->new; my $ec_dir = $mw->ExecuteCommand( -command => 'dir; sleep 10; dir;', -entryWidth => 50, -height => 10, -label => '', -text => 'Execute dir ', )->pack; my $ec_date = $mw->ExecuteCommand( -command => 'date; sleep 10; date;', -entryWidth => 50, -height => 10, -label => '', -text => 'Execute date ', )->pack; my $dir_but = $mw->Button( -text => 'Execute dir', -background => 'hotpink', -command => sub{ $ec_dir->execute_command })->pack; my $date_but = $mw->Button( -text => 'Execute date', -background => 'lightgreen', -command => sub{ $ec_date->execute_command })->pack; MainLoop;

    For a separate toplevel:

    #!/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