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

Kind Monks,

I'm trying to figure out how can I update the same line in a text widget. The purpose is to update the size of a certain file in a loop. My text widget is $textlog

$text_log->delete('end startline', 'end'); $text_log->insert('end startline', "Current Size:$FileSize"); $text_log->update; $text_log->see('end');

The problem is that each iteration the text is appended rather than replacing the former text.

Can you please help?

Replies are listed 'Best First'.
Re: Updating the same line Text Widget
by zentara (Cardinal) on Oct 20, 2010 at 12:19 UTC
    Try the method shown in inserting program output in Tk::Text

    You can also use indices ( see the section on Indices) in "perldoc Tk::Text". Essentially, you find the start indice of your word, count the number of characters in the word, delete ($indice + $n chars), the insert at the same $indice.

    If you show a working code example, we can probably show you how.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Updating the same line Text Widget
by jethro (Monsignor) on Oct 20, 2010 at 11:54 UTC
    According to Tk::Text (section "Indices") you might have meant 'startline.end' instead of 'end startline'

      When using 'startline.end' there is a Tk Error that the index is bad

        Then maybe 'startline' is nothing legal. I only found 'linestart' in the docu. 'end linestart' could actually work to mean the start of the last line in a text, if I read the documentation correctly

        If not, try something like '1.0' or 'x.0' with x the line number of the line where your filesize is put in the text widget

Re: Updating the same line Text Widget
by zentara (Cardinal) on Oct 20, 2010 at 14:41 UTC
    I made a little example for you, to show the difference between using indices and tags for doing the delete and insert. Tags work much better, because it tags the entire length of the inserted word, making inserts cleaner.

    Also note that there is a spelling error in the perldoc for Tk::Text, deleteTextWithTag should be DeleteTextWithTag.

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; my $update = 0; $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size => 14 ); my $text = $mw->Text(-font=>'big')->pack; $text->tagConfigure( 'updatetag', -borderwidth => 3, -background => 'yellow', -relief => 'raised', ); my $string = join ' ',('a'..'z'); foreach my $line(0..10){ $text->insert('end',"$line->$string\n"); } $text->delete("7.11", '7.13'); $text->insert("7.11", "$update ",['updatetag']); my $bFrame = $mw->Frame->pack(qw/-side bottom/); $bFrame->Button( -text => "Update Text", -command => sub { $update++; # this works, but adds spaces as $update gains characters # $text->delete("7.11", '7.13'); # $text->insert("7.11", "$update ",['updatetag']); #works good $text->DeleteTextTaggedWith('updatetag'); $text->insert("7.11", "$update ",['updatetag']); })->pack(qw/-side left/); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Updating the same line Text Widget
by jethro (Monsignor) on Oct 20, 2010 at 09:27 UTC
    It might help if you identified the GUI-module you are talking about.

      Sorry for forgetting this info - Tk

Re: Updating the same line Text Widget
by lamprecht (Friar) on Oct 20, 2010 at 20:57 UTC
    Hi,

    there is an invisible newline at the end of a Text widgets content. - Plus possibly one as part of the last line you inserted?

    use warnings; use strict; use Tk; my $mw = tkinit; my $text_log = $mw->Text->pack; $text_log->insert(end => "test\n") for (0..2); my $i = 0; $mw->repeat(500, sub{ $text_log->delete('end -2line linestart', 'end -1line' +); $text_log->insert('end', "Current Size:$i\n"); $i++; }); MainLoop;
Re: Updating the same line Text Widget
by Bintuch (Acolyte) on Oct 21, 2010 at 09:48 UTC

    Thanks guys! It works!

    I've implemented lamprecht method and thanks, zentare, for itroducing to me the 'taged' way