Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Tk: Set text size in a widget

by GrandFather (Saint)
on Nov 13, 2005 at 03:02 UTC ( [id://508059]=CUFP: print w/replies, xml ) Need Help??

This sub takes a single line Tk text widget reference (such as a Label) and a string then sets the font size in the widget to maximise the size of the text without clipping.

Place this in the resize handler for a widget to resize the text when the widget size changes.

sub SetBigText { my ($widget, $text) = @_; my $font = $widget->Font (); my $widthF = $widget->fontMeasure ($font, $text); my $heightF = $widget->fontMetrics($font, -linespace); my $xRatio = $widget->width / $widthF; my $yRatio = $widget->height / $heightF; my $minRatio = $xRatio < $yRatio ? $xRatio : $yRatio; my $fontSize = $widget->fontActual ($font, -size); my $newSize = $minRatio * $fontSize; $font->configure (-size => $newSize); $widget->configure (-text => $text); $widget->configure (-font => $font); }

Replies are listed 'Best First'.
Re: Tk: Set text size in a widget
by rcseege (Pilgrim) on Nov 13, 2005 at 08:28 UTC

    The posted code was interesting (++) - thanks for posting it. I played with it for a bit, and tried a few different things. It serves as a convenient way to highlight some minor issues with Tk's Font handling. As the snippet is written right now, I wouldn't suggest using it as an event handler. If it is called whenever Label is resized, it will introduce a small memory leak.

    The code in this post was only tested on a Windows installation. I'd be interested to hear if it works differently on other platforms.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found