Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Tk Formatting

by NateTut (Deacon)
on Oct 22, 2009 at 18:59 UTC ( #802784=perlquestion: print w/replies, xml ) Need Help??
NateTut has asked for the wisdom of the Perl Monks concerning the following question:

The subroutine below attempts to create a Tk window sized to closely fit the text in @ChapterText. It was developed in the try and try again method and is kludgy. It mostly works, but sometimes it leaves too much white space at the bottom of the window.

Is there a way to size a Tk window to fit it's contents?

sub DisplayChapter { my ($Chapter) = shift; my @ChapterText = GetChapter($Chapter); my $MaxLineLength = 0; foreach(@ChapterText) { if(length($_) > $MaxLineLength) { $MaxLineLength = length($_); } } my $GeometryString = ($MaxLineLength * 8) . 'x' . (scalar(@Chapter +Text) * 21); $mw->geometry($GeometryString); $mw->title(' ' x ((($MaxLineLength - 2) / 2) + 5) . (sprintf('%7s', + Number2Roman($Chapter)))); $OutputText->delete('1.0', 'end'); $text->tagConfigure(centered => -justify => 'center'); foreach my $Line (@ChapterText) { $text->insert('end', $Line, 'centered'); } $OutputText->pack(qw/-side bottom -fill both -expand 1/); }

Replies are listed 'Best First'.
Re: Tk Formatting
by keszler (Priest) on Oct 22, 2009 at 19:21 UTC
    For many widgets, $widget->height($widget->reqheight) will work. (and also s/height/width/g).

    Assuming that you're using some form of a Tk::Text for the @ChapterText, I'd suggest using the textbox's height and width (which set the widget size based on the font used) and then resize the MainWindow based on $text->reqheight and $text-reqwidth.

Re: Tk Formatting
by lamprecht (Friar) on Oct 22, 2009 at 21:41 UTC

    maybe a Tk::Message widget would make that easier:

    perl -MTk -e"tkinit->Message(-text=>qq/Multiline Text \n/x5,-justify => 'center')->pack; MainLoop"
    Cheers, Christoph

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://802784]
Approved by Argel
[stevieb]: ...or the proper download link and release page even...
[pryrt]: da39a3ee5e6b4b0d32 55bfef95601890afd8 0709 *strawberry-perl-5 .12.3.0-portable. zip
[pryrt]: matches neither. :-)
[stevieb]: wtf!? lol
[stevieb]: I'm checking some other versions. This is a little frightening
[pryrt]: Sorry, my bad: it was't done downloading when I did the sha1sum... now I match your 309ad... value

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-03-29 21:00 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.