I'm using Text widgets in a TableMatrix table to display text, and I'm trying to set correct row heigths with word wrap.
Essentially, I need some way to query the Text widget and find out how many lines it's been broken up into, so that I can set the correct row height in TableMatrix. You can't query that directly, but there may be a trick that works here... I just can't find it. I can't even get some of the features of Tk::Text to work: dlineinfo and bbox fail for me with any index other than 1.0. If I could get the position of the first and last character with bbox, maybe I could calculate the height.
Here's some of my stabs in the dark:

use strict; use warnings; use Tk; use Tk::TableMatrix::Spreadsheet; my $mw = Tk::MainWindow->new; $mw->geometry('400x300'); # loaded from setup if given, defaults to + 1000x600 my %arrayVar; my $arrayVar = \%arrayVar; # Ref to hash my $t = $mw->Spreadsheet( -rows => 1, -cols => 2, -width => 16, -variable => $arrayVar, )->pack(-expand => 1, -fill => 'both');; $t->rowHeight(0, 6); $arrayVar->{"0,1"} = "some text directly inserted into the cell"; my $tw = $mw->Text( -wrap => "word"); $tw->insert("1.0", "here is some text in a Text widget; some more text + some more text some moooore text and then some more text"); $tw->tagConfigure('highlight', -background => "yellow"); $tw->tagAdd('highlight', '1.23', '1.27'); # can also use 'end' and 'l +ineend' $tw->pack(); # loose text widget for testing # $t->windowConfigure("0,0", -window => $tw); # -sticky => 's', #p +ut the Text widget in the tablematrix table my $cgetheight = $tw->cget(-height); print "height based on cget $cgetheight"; my ($x,$y,$w,$h,$b)=$tw->dlineinfo('1.0'); print "\nHeight based on dlineinfo: $h"; my $widgetheight = $tw->height; print "\nWidget height: $widgetheight\n"; my ( $width, $height, $deltax, $deltay ) = split /[+x]/, $tw->geometry +; print "Height based on geometry: $height\n"; ($x,$y,$w,$h)=$tw->bbox('1.0'); print "X: $x\n"; print "Y: $y\n"; print "W: $w\n"; print "Height based on bbox: $h\n"; MainLoop;

In reply to Query the height of Tk::Text widget with word wrap by elef

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.