Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: TK-Text Get text per row

by keszler (Priest)
on Sep 10, 2013 at 15:41 UTC ( #1053296=note: print w/ replies, xml ) Need Help??


in reply to TK-Text Get text per row

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.


Comment on Re: TK-Text Get text per row
Select or Download Code
Re^2: TK-Text Get text per row
by choroba (Abbot) on Sep 10, 2013 at 21:00 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053296]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2014-10-01 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (386 votes), past polls