I'd like to do is to make the Text widget only as large as I need to given the number of lines that are in the widget at the time.

I don't think that should be too hard, since the text widget's height and width options are in terms of lines and character width. If you say -height=>4, -width=>25, that is 4 lines and 25 characters.

I havn't tried a $mw configure yet, but as esserte said, you may run into an infinite feedback loop if you try to reconfigure the $text widget as the $mw is being resized. You might be able to work out a flag, that gets set when the $mw resize starts, and is unset on the Button-1 Release. Then do your $text resize after the $mw resize is finished.

But back to the point of pixel size -vs -character size, look at this script, you don't need to worry about pixels. I didn't adjust for line width, but all you would need to do is loop through each line, take it's 'length' then reconfigure your $text widget -width option to the max length.

P.S. I guess there would be some trouble if you went to a very small window size, and you needed to search for a font that would fit and still be legible. All systems don't have the same fonts installed, so that is another problem.

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; $mw->fontCreate('big', -family=>'courier', -weight=>'bold', -size=>int(-18*18/14)); my $tx = $mw->Text( -height => 4, #initialy set it small -width => 25, )->pack(); foreach (1 .. 20 ) { $tx->insert( 'end', "line$_\n" ); } $tx->insert('end', "Blah " x 10); my $end = $tx->index('insert'); #get last line $tx->configure(-height=>$end); my $button = $mw->Button(-text=>'Reconfigure', -command=>sub{ $tx->configure(-font => 'big'); $tx->update; $tx->configure(-height => $end); }, )->pack(); MainLoop;

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re: Tk:Text resize dillema by zentara
in thread Tk:Text resize dillema by gri6507

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.