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

Dear monks
I am trying to do a autoscroll with a text widget like this:
sub display_text { my $txt_summary = $bottomFrame->Scrolled( "text", -background => 'blue', -foreground => 'white', -height => '1', -takefocus => '0', -width => '40', -scrollbars => 'oe', )->pack; open(FH, "cuentos.txt") || die "no Abrio nada"; while (<FH>) { $txt_summary -> Insert($_); } close (FH); $txt_summary->see('end'); return $txt_summary; }
but I am unable to make the text move why?
Thank you dear monks

Replies are listed 'Best First'.
One Answer (Re: How can I get indefinite scroll to work in a TK text widget)
by shmem (Chancellor) on May 16, 2008 at 17:20 UTC

    Bad title. Bad title!

    That said - It Works For Me TM.

    use Tk; $bottomFrame = MainWindow->new(); sub display_text { my $txt_summary = $bottomFrame->Scrolled( "Text", -background => 'blue', -foreground => 'white', -height => '1', -takefocus => '0', -width => '40', -scrollbars => 'oe', )->pack(qw(-fill both -ex +pand 1)); open(FH, "cuentos.txt") || die "no Abrio nada"; while (<FH>) { $txt_summary -> Insert($_); } close (FH); $txt_summary->see('end'); return $txt_summary; } display_text(); MainLoop; __END__

    Maybe it's context? is it the missing arguments to pack()? what kind of object is $bottomFrame actually? Also, "text" ought to be "Text".

    thanks to roboticus for the inspired title

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Hi!
      my $bottomlowframe = $mw->Frame()->pack(-fill=>'x');
      it just a Frame
      but I think I ask the wrong question and I am sorry:(
      I want that the Text widget continue to scroll the text "undefinitly" or "forever" but at a reasonable speed to be able to read it, I hope it makes more sense. you know like those things that keep showing the news on the tv but I want it in my Tk windows
      Thank you

        Ah. Makes more sense. While perl is busy executing display_text(), the GUI doesn't get updated. You need Tk::after or ::repeat for that:

        use Tk; $bottomFrame = MainWindow->new(); my $txt_summary = $bottomFrame->Scrolled( "Text", -background => 'blue', -foreground => 'white', -height => '1', -takefocus => '0', -width => '40', -scrollbars => 'oe', )->pack(qw(-fill both -ex +pand 1)); open(FH, "cuentos.txt") || die "no Abrio nada"; sub display_text { if(eof(FH)) { $bottomFrame->afterCancel($bottomFrame->{RepeatID}); close (FH); $txt_summary -> Insert("\n== FIN ==",'end'); return; } else { chomp(my $line = <FH>); $txt_summary -> Insert("\n$line",'end'); } $txt_summary->see('end'); } $bottomFrame->{RepeatID} = $bottomFrame->repeat(500, \&display_text); MainLoop; __END__

        But probably you want Tk::fileevent. Note also that "Scrolled" means "attach a scrollbar to it", and not "scroll this widget".

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: How can I get indefinite scroll to work in a TK text widget
by zentara (Cardinal) on May 16, 2008 at 20:02 UTC
    You might be better off going to a Canvas widget for a smooth indefinite scroll, like for a teleprompter.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->geometry($mw->screenwidth . 'x' . $mw->screenheight ); $mw->title("Scrolling text ----- Click to exit"); my $canvas = $mw->Canvas( -height => $mw->screenheight, -width => $mw->screenwidth, -background => 'black', -relief => 'raised' )->pack(); $mw->fontCreate( "fonta", -family => 'arial', -size => -30, -weight => 'bold' ); my $adjust; while (<DATA>){ chomp; $adjust += 50; $canvas->createText(50,$mw->screenheight + $adjust, -text => $_, -fill =>'hotpink', -anchor => 'nw', -font => 'fonta', -tags => ['mover'], ); } $mw->bind( '<ButtonPress-1>', sub { exit; } ); my $id = $mw->repeat(20, sub{ $canvas->move('mover', 0, -1);} ); MainLoop; __DATA__ Attack of the Microsith Not so long ago, in a computer not so far away, the evil lord of the Microsith, Darth Bill, was conspiring with the enemies of Open Source Federation to take complete control over all motherboards in the known universe. It was during this monstrous conspiracy, that Prince Linus had the revelations from the Force, to create a new electron-manipulation-engine that would be so clean and powerful, that it would thwart Darth Bill's evil plans. In a fortunate turn of fate, a young Jedi named Larry, came upon a powerful technique of controlling electron-flow which would free everyone from the dependency on Darth Bill's polluting technology forever. Darth Bill became aware of a planned meeting between Prince Linus and the young Jedi Larry, and assembled his forces led by the dark lord Daryl, to mount a campaign of harrasment against the Federation. Our story begins.............

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Hi
      Thanks for the idea and taking the time to write a program to explain, one thing, I copy your program and run it and notice it did not cycle the text, what am i missing? tell you the truth I am a newbie on Tk, what it takes to make it cycle inifinite
        To make the text cycle again as it finishes, is simple. The idea is to get the position of the text block by testing it often, when the lower part of the block goes above the top of the screen, move the whole text block back down the amount it is out of view.

        A super simple scrolling marquee can be made with

        #!/usr/bin/perl use Tk; use strict; my $text = 'Hello World !! '; my $mw = tkinit; $mw->geometry('+200+200'); $mw->overrideredirect(1); my $label = $mw->Label( -textvariable=>\$text, -font=>'courier', -bg=>'green', -bd=>4, -relief=>'ridge' )->pack(-fill=>'both'); $label->bind('<ButtonRelease>',sub{$mw->destroy}); # a simple regex that is hard to explain :-) # a clever trick $mw->repeat(60,[sub{$text=~s/(.)(.*)/$2$1/;}]); MainLoop;

        but here is how you would do my canvas example, and you can adjust spacing and speed as desired. One thing to watch for, is the juggling of the repeat rate, and distance moved. Too fast a repeat rate may cause slow machines to jitter, so slow repeat rate and increase each y move until a smooth flow is attained.

        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->geometry($mw->screenwidth . 'x' . $mw->screenheight ); $mw->title("Scrolling text ----- Click to exit"); my $canvas = $mw->Canvas( -height => $mw->screenheight, -width => $mw->screenwidth, -background => 'black', -relief => 'raised' )->pack(); $mw->fontCreate( "fonta", -family => 'arial', -size => -30, -weight => 'bold' ); my $adjust; while (<DATA>){ chomp; $adjust += 50; $canvas->createText(50,$mw->screenheight + $adjust, -text => $_, -fill =>'hotpink', -anchor => 'nw', -font => 'fonta', -tags => ['mover'], ); } $mw->bind( '<ButtonPress-1>', sub { exit; } ); my ($x1,$y1,$x2,$y2) = $canvas->bbox('mover'); my $y2_init = $y2; #save original offset print "$y2_init\n"; my $id = $mw->repeat(20, \&mover); MainLoop; sub mover { $canvas->move('mover', 0, -1); my ($x1,$y1,$x2,$y2) = $canvas->bbox('mover'); print "$x1 $y1 $x2 $y2\n"; # reset if ($y2 < 0){ $canvas->move('mover', 0, $y2_init) } } __DATA__ Attack of the Microsith Not so long ago, in a computer not so far away, the evil lord of the Microsith, Darth Bill, was conspiring with the enemies of Open Source Federation to take complete control over all motherboards in the known universe. It was during this monstrous conspiracy, that Prince Linus had the revelations from the Force, to create a new electron-manipulation-engine that would be so clean and powerful, that it would thwart Darth Bill's evil plans. In a fortunate turn of fate, a young Jedi named Larry, came upon a powerful technique of controlling electron-flow which would free everyone from the dependency on Darth Bill's polluting technology forever. Darth Bill became aware of a planned meeting between Prince Linus and the young Jedi Larry, and assembled his forces led by the dark lord Daryl, to mount a campaign of harrasment against the Federation. Our story begins.............

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: How can I get indefinite scroll to work in a TK text widget
by Anonymous Monk on May 16, 2008 at 17:53 UTC

    Hi Padawan. Please use a more descriptive title next time. For this post, a good title might be "autoscroll Tk text widget"

    The reason is, we want these threads to be easily searchable for those who come after us. :)