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

I have a Tk-Text Widget with -width => 50 and -wrap => 'word'.

Can i find out how many rows are in the Widget and can i get single rows?

I want to read the rows to a string and add a Newline after everey row.

Replies are listed 'Best First'.
Re: TK-Text Get text per row
by Sandy (Curate) on Sep 10, 2013 at 21:37 UTC
    I'm not sure if this helps any, but I found it in the Tk docs
    When moving up or down a certain number of lines, this is interpreted as logical lines, where each line is terminated only by the "\n". With long lines and wrapping enabled, this may be represent multiple lines on the display. If you'd like to move up or down a single line on the display, you can specify this as e.g. "1.0 + 2 display lines".
    I can't get it to work, but I have an older version of Tk. Hopefully it will work for you.
Re: TK-Text Get text per row
by keszler (Priest) on Sep 10, 2013 at 15:41 UTC
    From Tk::Text:
    $text->index(index)
    Returns the position corresponding to index in the form line.char where line is the line number and char is the character number. Index may have any of the forms described under "INDICES" above.
    So $text->index('end') should return the line.char of the last character. To get each wrapped row, something like:
    $text->get("${row}.0","${row}.end") for each line should work.
      Unfortunately, it returns the lines as defined in the original contents, not in the wrapped form:
      #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = 'MainWindow'->new; my $t = $mw->Text(-width => 40, -wrap => 'word')->pack; my $b = $mw->Button(-text => 'Show', -command => sub { my ($last_line) = $t->index('end') =~ /(.*)\. +/; for my $line (1 .. $last_line) { print "$line: ", $t->get("$line.0", "$lin +e.end"), "\n"; } })->pack; $t->Contents(join ' ', split /\n/, <<'EOF'); Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). Its major features are that it's easy to use, supports both procedural and object-oriented (OO) programming, has powerful built-in support for text processing, and has one of the world's most impressive collections of third-party modules. Different definitions of Perl are given in perl, perlfaq1 and no doubt other places. From this we can determine that Perl is different things to different people, but that lots of people think it's at least worth writing about. EOF MainLoop();

      Output:

      1: Perl is a general-purpose programming language ... 2:
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Unfortunately, it returns the lines as defined in the original contents, not in the wrapped form:

        That makes sense, wrapping doesn't count -- it appears there is no way to retrieve the position of the wraps. You can get the space (in pixels) a line takes up with https://metacpan.org/module/Tk::Text#text-dlineinfo-index but that's about it