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

Dear fellow nuns and monks,

I wrote a program using PERL/Tk which processes data. The processing takes some while, so I create status messages and display them in a scrolled textbox. This works very fine, but after some time the amount of status message lines is bigger than the number of lines contained in the text windows - exactly the situation for which a scrolled box is required.

Now, the status messages are still added in the right way, but they are not visible on the screen, because the text box only displays the top portion of the text, whereas to view those lines at the end of the text (and therefore the most recent status messages) one has to scroll manually down the text.

What I would like to have now is that the text window automatically scrolls down, so that always the bottom part of the text is visible, and so the most recent status messages.

How can I program that behaviour?

Many thanks in advance for your help!

With my best greetings and wishes,
Yours

chanklaus

Learning is like swimming against the current - as soon as you stop you'll drift back
(Chinese proverb)
  • Comment on PERL/Tk Scrolled Textbox: How do I tell it to automatically scroll down?

Replies are listed 'Best First'.
Re: PERL/Tk Scrolled Textbox: How do I tell it to automatically scroll down?
by desemondo (Hermit) on Oct 02, 2009 at 11:27 UTC
    How about turning your problem on it's head?

    You may have already thought of this, or you might simply hate the idea - but how about printing the most recent items onto the front of the list instead of the end?

    In other words, you'd only need to scroll the list to view the history, instead of the scrollbox scrolling to keep up with the most current event...

    FWIW, I much prefer this behaviour for any sort of monitoring/status screen. But, each to their own though, I spose...
Re: PERL/Tk Scrolled Textbox: How do I tell it to automatically scroll down?
by biohisham (Priest) on Oct 02, 2009 at 10:36 UTC
    use the Scrolled constructer, it can be used to add scrollbars to widgets easier and positioning them using compass orientations is more direct forward as well as they're inherently automatic and appear when needs be. This is better and more concise than the "Scrollbar" method which requires further configuration afterwards to become automatic and so "Scrolled" rules.

    Assign the text widget value to a scalar and proceed from there:

    #assuming this is a part of a bigger Tk program.. ... my $textWidget=$main->Text( -height=>'20', -width=>'10' )->pack; $textWidget=$main->Scrolled( 'Text', scrollbars=>'w' #this can be n,s,e,w according to compass po +sitions. )->pack; ... MainLoop;
    Update: Anyways, I don't know if this sounds easy to do or not but I guess Tk::mega can give you an insight check perldoc for it, also as has been listed in the answers $text->see{INDEX} seems to be what you need check the link provided, it is so informative, other than that run the Tk module command widget from the console, it has many other things that I hope can be helpful.

    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
      Hi, the question was how to have it automatically scroll to the end
      #!/usr/bin/perl -- use strict; use warnings; use Tk; my $mw = tkinit(); my $l = $mw->Scrolled(qw' Text -height 2'); $mw->Button( -text => 'add', -command => sub { $l->insert( 'end', localtime() . "\n" ); $l->see('end'); } )->pack; $l->pack; $l->insert( 'end', "hi\nthere\n" ); MainLoop();
Re: PERL/Tk Scrolled Textbox: How do I tell it to automatically scroll down?
by Anonymous Monk on Oct 02, 2009 at 09:11 UTC
Re: PERL/Tk Scrolled Textbox: How do I tell it to automatically scroll down?
by chanklaus (Acolyte) on Oct 05, 2009 at 07:34 UTC
    The

    $text->see('end');

    line turned the trick!

    Many thanks to everybody!

    chanklaus

    Learning is like swimming against the current - as soon as you stop you'll drift back
    (Chinese proverb)
Re: PERL/Tk Scrolled Textbox: How do I tell it to automatically scroll down?
by Anonymous Monk on Oct 02, 2009 at 14:36 UTC
    There is a function available for you . $text->see('end');