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

Dear monks

I thought it should be easy to achieve this...but after reading both Entry and ListBox PODs I could not found any solution. I am creating a mega widget with an Entry and a ListBox. What I need is to get the size (better in pixels, but characters would go too) and set the same width to the ListBox.

I seem to fail already by the first task, reading the width of the Entry. A simple

$EntryWidget->cget('-width');

Does not seem to help. I have the impression I get a standard "20" which may be the value at creation time (but not the real width of the widget which is managed by pack).

Maybe this task cannot be achieved, or maybe I am looking at the wrong approach. Any suggestions will be appreciated.

Replies are listed 'Best First'.
Re: Tk Entry and ListBox width
by IB2017 (Pilgrim) on Sep 23, 2019 at 09:55 UTC

    To get the size in pixels of a widget, use:

    $EntryWidget->width;

    UPDATED: As far as the ListBox is concerned, set geometry of the containing widget (if a Toplevel window), which allows for setting size in pixels, with:

    $window->geometry();

    or simply play with the options (-extend and -fill) of pack for both the containing widget (probably a Frame) and of the ListBox.