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

I tried to determine the height of a label object, but every time I run the code it returns '1', unless I run it using the Tk debugger (perl -d:ptkdb script.pl) in which case it returns '20', which is the correct value.

This is the code:
#!/usr/bin/perl -w use strict; use Tk; my $main = MainWindow->new(-background => "white"); $main->title("test program"); my $mainframe = $main->Frame(-background => "white")->pack(); my $label=$main->Label(-text => "test text", -background => "white")-> +pack; print $label->height(); MainLoop()

So my obvious question is why I don't get the correct value ?!?!

Jouke Visser, Perl 'Adept'

Replies are listed 'Best First'.
Re: Height of a label widget in Tk
by busunsl (Vicar) on May 25, 2001 at 13:45 UTC
    The call to MainLoop will pack the window and calculate the size of the widgets.
    Create a Button to call a sub, it will give you the correct height.

    The Tk-Debugger has it's own MainLoop, well sort of. So height will be printed correctly.

    Update:
    Putting a $main->update() before the print will work.

      Your last suggestion works like a charm! Thanx! ++busunsl!

      Jouke Visser, Perl 'Adept'
Re: Height of a label widget in Tk
by TheoPetersen (Priest) on May 25, 2001 at 17:04 UTC
    In general you can't trust the values of attributes like this until the widget (or window) is displayed. The $w->waitVisibility method allows you to let the widget get displayed and then check matters of interest.

    Inside of $w->waitVisibility Tk does updates and processes other events until the widget receives a Visibility event. If you want to handle this kind of thing asynchronously you could look at hooking into that event yourself.