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

I have a script written in perl and would like to run this script in a window/tab via perl/tk. Basically I want the perl/tk main window with possibly some tabs. Then in a lower pane window show the progress of my perl script. When the script requires user input I would then have the perl/tk generate a popup window box for user input back to the main script. Is this possible with perl/tk, any documentation I can read or example?

Replies are listed 'Best First'.
Re: perl/tk show script progress
by zentara (Cardinal) on Sep 08, 2011 at 18:45 UTC
    What you ask for is possible, but is very complex, and much depends on the nature of the Perl script you are running. How can you tell what the progress is on your script? Does the script print out the percent done? There are Notebook tabbed widgets, progressbar widgets, and IPC modules that you can use, and you can google for many examples previously posted. But first you need to learn something about running Tk, see perltk tutorial and another tutorial . You can also read the good book, the bible of Tk, Mastering Perl/Tk.

    It all seems overwhelming at first, but it all starts with writing a "Hello World" program.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      I do not really want a progress bar displaying the progress of my script. I want my second script to run within the GUI window showing the scripts progress, meaning the printing of data and so forth the second script executes.
Re: perl/tk show script progress
by Khen1950fx (Canon) on Sep 08, 2011 at 19:20 UTC
    I added zentara's script to mine and got this:
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::NoteBook; my $starttime; my $raisetime; my $progressbar; my $percent_done; my $mw = MainWindow->new(); $mw->geometry("400x100"); my $book = $mw->NoteBook()->pack( -fill => 'both', -expand => 1 ); my $tab1 = $book->add( "Sheet 1", -label => "Start", -createcmd => \&getStartTi +me ); my $tab2 = $book->add( "Sheet 2", -label => "Continue", -raisecmd => \&getCurre +ntTime ); my $tab3 = $book->add( "Sheet 3", -label => "Progress", -createcmd => \&getProg +ressBar ); my $tab4 = $book->add( "Sheet 4", -label => "Command", -createcmd => \&getExecu +teCommand ); my $tab5 = $book->add( "Sheet 5", -label => "End", -state => 'disabled +' ); $tab1->Label( -textvariable => \$starttime )->pack( -expand => 1 ); $tab2->Label( -textvariable => \$raisetime )->pack( -expand => 1 ); $tab3->Button( -text => 'Quit', -command => sub { exit; } ) ->pack( -expand => 1 ); MainLoop; sub getStartTime { $starttime = "Started at " . localtime; } sub getCurrentTime { $raisetime = " Last raised at " . localtime; $book->pageconfigure( "Sheet 3", -state => 'normal' ); } sub getProgressBar { my $mw = MainWindow->new( -title => 'ProgressBar example' ); my $progress = $mw->ProgressBar( -width => 30, -from => 0, -to => 100, -blocks => 50, -colors => [ 0, 'green', 50, 'yellow', 80, 'red' ], -variable => \$percent_done )->pack( -fill => 'x' ); $mw->Button( -text => 'Go!', -command => sub { for ( my $i = 0 ; $i < 1000 ; $i++ ) { $percent_done = $i / 10; print "$i\n"; $mw->update; } } )->pack( -side => 'bottom' ); MainLoop; } sub getExecuteCommand { 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; }
      Much appreciated!! Looks better, but I will still need to work on this to make my second perl script to execute and run in a GUI window. I'd really like to get my second perl script to run within the same window as the main GUI, such as, one of the tabs. Essentially one large GUI window at the top some buttons and at the bottom an inset window where my second perl script would run.

        Hi,

        Not sure why you need a second script, could you not use a sub kicked off by a button?

        I think you want to print text messages 'log style', you could use a scrolled-textbox for this. Check for it in the Tk manual.

        J.C.

        Or perhaps a listbox, non-selectable, might do the trick.

        J.C.

Re: perl/tk show script progress
by Khen1950fx (Canon) on Sep 08, 2011 at 18:16 UTC
    I googled and found this:

    The ProgressBar Widget:

    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::ProgressBar; my $percent_done; my $mw = MainWindow->new( -title => 'ProgressBar example' ); my $progress = $mw->ProgressBar( -width => 30, -from => 0, -to => 100, -blocks => 50, -colors => [ 0, 'green', 50, 'yellow', 80, 'red' ], -variable => \$percent_done )->pack( -fill => 'x' ); $mw->Button( -text => 'Go!', -command => sub { for ( my $i = 0 ; $i < 1000 ; $i++ ) { $percent_done = $i / 10; print "$i\n"; $mw->update } } )->pack(-side => 'bottom'); MainLoop;
      This helps, yet I want a perk/tk gui window and within the window or a tab show a textarea type box where my second perl script will run.
        yet I want a perk/tk gui window and within the window or a tab show a textarea type box where my second perl script will run.

        See Tk::ExecuteCommand. Here is an example:

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

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
        The ProgressBar would be good if I could show the number count within the perl/tk GUI instead of the terminal window I started the ProgressBar perl/tk script.