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

Hi Pals..... I have an issue with creating an indicator using Tk. The issue goes like this: My current application is already using Tk to display a window but my application consumes around one minute and finally displays the result in that window. It means that I am already calling MainLoop() to display this window which is an infinite loop. I want to display a progressbar for that one min gap. I am unable to use Tk::ProgressBar since calling MainLoop for progressbar will cause my application to stuck at that point. So , I guess i cannot call MainLoop twice in a single application. To summarise: -> I need to display a some GUI indicator(progress bar) before displaying my result window. Can I do something to the window (like append messages) even after MainLoop statement. Issue is , since I am already calling MainLoop for displaying window, I am wondering how can I queue up other events like adding text messages to the same window. So suggest me some standard module which should not use Tk to create some sort of indicator... OR LET ME KNOW HOW I CAN APPEND MESSAGES TO THE EXISTING WINDOW... I SHOULD BE ABLE TO ADD TEXT MESSAGES EVEN AFTER INVOKING MAINLOOP(). Chk the psuedo-doe below and let me know if I can d something in line 5 to the created window??? PSUEDO-CODE:
1-create MainWindow; 2-give title; 3-add widgets; 4-MainLoop(); 5- Can I add messages to the above created window in this line.
Thnnx guys.

Replies are listed 'Best First'.
Re: creating simple GUI
by GrandFather (Saint) on Oct 31, 2005 at 02:35 UTC

    MainLoop doesn't exit until you close the top level window widget. However the code that is executed from MainLoop can call $main->update (); to get the GUI refreshed. That is more likely what you want to do in any case if you are reporting progress.


    Perl is Huffman encoded by design.
      Thnx for the reply GrandFather.... suppose I have this in my code:
      use Tk; my $main=new MainWindow; $main->title("Test window"); <I print a message "msg" in the window at this line>; MainLoop;
      Now how do I add another message to the window.. ? My requirement is.. I should update the window after the MainLoop statement. Just let me know how exactly to use UPDATE()??

        I don't know how you show your message, but the code below updates the title bar of the main window to "show progress"

        use warnings; use strict; use Tk; my $main= MainWindow->new (-title => "Test window"); $main->update (); sleep (2); $main->title ("Message"); $main->update (); sleep (2); $main->title ("Startup done"); MainLoop;

        Perl is Huffman encoded by design.
Re: creating simple GUI
by Sandy (Curate) on Oct 31, 2005 at 14:58 UTC
    #!/usr/bin/perl use strict; use Tk; use Tk::ProgressBar; # make window my $mw = new MainWindow(); # tell MainWindow to execute this function after 1000 milliseconds $mw->after(1000,\&MyUpdate); # or tell MainWindow to execute this sub repeatedly $mw->repeat(1000,\&MyOtherUpdate); MainLoop; sub MyUpdate { # ... start something? } sub MyOtherUpdate { # ... update window, reset progress bar? }

    Untested!

    PS If you use the progress bar, you must remember to update the window (via $mw->update())

Re: creating simple GUI
by spatterson (Pilgrim) on Oct 31, 2005 at 13:18 UTC
    hmmm,
    use Tk; my $mw = new Tk::MainWindow; my $frame = $mw->Frame->pack; $mw->update(); while () { $frame->Label(' foo? '); $mw->update(); sleep 5(); } MainLoop;
    just another cpan module author
Re: creating simple GUI
by zentara (Cardinal) on Oct 31, 2005 at 12:04 UTC
    You really should tell us a little more about the program which you are running for the minute, for which you want to display a progressbar. There are a few better options which you are unaware of, but they depend on the behavior of the long-running app. Does your app output anything that can be used to indicate progress, or when it has finished? If so, you can run it thru IPC::Open3, a piped open, or even in a thread, and you can get a very nice progress bar. Tell us more, or we can only guess, and tell you clunky solutions.

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