Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Size of text in perlTk button

by blueberryCoffee (Scribe)
on Jan 14, 2005 at 09:28 UTC ( #422179=perlquestion: print w/replies, xml ) Need Help??

blueberryCoffee has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a button that expands to fill the window but if the window is small then the text is cut off at both ends. So a button that should read:
"Send command XXXX to system YYYYY"
shows as "mand XXXX to syst".
I want to know if anyone knows of a way to check if the text can be displayed so that I can change the text to:
"Send command XXXX..."
I looked in docs, MTK, and google. I know in raw X you can do this but on Windows with perlTK I just don't have a clue where to go.

Thanks in advance

Replies are listed 'Best First'.
Re: Size of text in perlTk button
by zentara (Archbishop) on Jan 14, 2005 at 14:43 UTC
    In addition to the idea of aligning your text to the upper left corner, you could put a newline(s) in your text, so that the "Send command XXXX\nto system YYYY" will clip the top line last. Also, you can declare your button text string before you build( or configure) the button, take the length of the text, and use the -width=>$length option. Think creatively. :-) You could also reconfigure your button text, whenever an resize takes place.
    #!/usr/bin/perl use Tk; my $mw = new MainWindow; my $currentSize = $mw->reqwidth . "x" . $mw->reqheight; $mw->bind( '<Configure>', [ \&OnResize, \$currentSize ] ); MainLoop; sub OnResize { my ( $mw, $oldSize ) = @_; my $newSize = $mw->width . "x" . $mw->height; if ( $$oldSize ne $newSize ) { ## Resize has occurred do something: printf( "Resize happened - old size: %s, new size: %s\n", $$oldSize, $newSize ); ## set the old size to the new size $$oldSize = $newSize; } }

    I'm not really a human, but I play one on earth. flash japh
Re: Size of text in perlTk button
by aquarium (Curate) on Jan 14, 2005 at 12:46 UTC
    use a scroll bar under the button OR instead have a text box with a scroll bar and just have a small button after the text to "execute/submit/etc" the command. i think the latter solution is more friendly.
    the hardest line to type correctly is: stty erase ^H
Re: Size of text in perlTk button
by Anonymous Monk on Jan 14, 2005 at 11:37 UTC
    I think there is a way to check how wide a text is using a particular font - but I don't have my Tk books here so I can't check. I might be totally wrong.

    There might however be a simpler solution that does what you want. Try aligning your text in the button. By default, Tk will center text (and hence, if the widget is too small, only display the center part). If you tell Tk to put the text on the left, it will show the left part of the text if it gets clipped. Unfortunally, this has the drawback that if the text is smaller than the button, it won't be centered. If that's not what you want, disregard this suggestion.

Re: Size of text in perlTk button
by samizdat (Vicar) on Jan 14, 2005 at 19:41 UTC
    Look at the base Widget code in man TK::Widget.

    $widget->geometry gives widthxheight+x+y in pixels. Even a brute-force examination of this should allow you to re-size the font or re-configure the text string for the size of the button as displayed. While it would be nice to use the exact pixel-used count, most apps don't need that fine a control and can't waste the time.
Re: Size of text in perlTk button
by elwarren (Priest) on Jan 14, 2005 at 17:27 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://422179]
Approved by Corion
Front-paged by Courage
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2023-01-31 10:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found