http://www.perlmonks.org?node_id=643687

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

I may be missing something hugely obvious here, but in the following simple test case:

use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $frame = $mw->Frame->pack(); populate(); $frame->Button(-command=>\&recompose)->pack(); MainLoop; sub recompose { $frame->destroy(); $frame = $mw->Frame->pack; populate() } sub populate { for (1..10) { $frame->Label(-text=>$_)->pack(); } }

clicking the button makes the main window resize itself to absolute minimal size (so the numbers can't be seen). To see the numbers the window has to be manually resized. Is this supposed to happen? Is there a call I can make to get the MainWindow to programmatically resize based on its new contents?

TIA

Update: liverpole has pointed out by /msg that he isn't having this problem, and at that point I suddenly remember I'm running cvs Tk on Perl 5.9.5, which might explain the odd behaviour (OS is Debian Etch). Does anyone else not see the numbers 1..10 after clicking on the button? I've reworded the question a bit to try and make the problem clearer.

Update: zentara has rightly pointed out that I probably shouldn't be doing this anyway.

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
John Brunner, "The Shockwave Rider".

Replies are listed 'Best First'.
Re: Tk Mainwindow resizing after child destroyed
by zentara (Archbishop) on Oct 09, 2007 at 15:29 UTC
    I see the problem using Slackware 12, and Tk-804.027. I experimented a little bit, and found a hacked solution.
    #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $frame = $mw->Frame->pack(); populate(); $frame->Button(-command=>\&recompose, -text=>'do_it')->pack(); MainLoop; sub recompose { $mw->withdraw; $frame->destroy(); $frame = $mw->Frame->pack(); populate(); $mw->packPropagate; $frame->update; $mw->update; $mw->deiconify; } sub populate { for (1..10) { $frame->Label(-text=>$_)->pack(); } }
    I will point out that your code is bad programming, because you are trying to clean out your labels by destroying the parent frame, still leaves the old labels laying around, and this will cause a memory gain everytime you do it. Using $widget->destroy usually is a bad thing in Tk. You may be able to get away with it, if you run packForget on all children of the frame, then destroy all the children. You are better off, NOT destroying the frame, just empty it then refill it.
    #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $frame = $mw->Frame->pack(); populate(); $frame->Button(-command=>\&recompose, -text=>'do_it')->pack(); MainLoop; sub recompose { my @w = $frame->packSlaves; print @w,"\n"; foreach (@w) { $_->packForget; $_->destroy;} populate(); } sub populate { for (1..10) { $frame->Label(-text=>$_)->pack(); } }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      I will point out that your code is bad programming, because you are trying to clean out your labels by destroying the parent frame, still leaves the old labels laying around, and this will cause a memory gain everytime you do it. Using $widget->destroy usually is a bad thing in Tk. You may be able to get away with it, if you run packForget on all children of the frame, then destroy all the children. You are better off, NOT destroying the frame, just empty it then refill it.
      /me slaps head. Never thought of that - thanks. Back to the drawing board to do it properly :-) and since this is AFAICT at least the second time I've been corrected on that, I've added it here: Re: Stupid mistakes I repeatedly make

      --------------------------------------------------------------

      "If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
      John Brunner, "The Shockwave Rider".

Re: Tk Mainwindow resizing after child destroyed
by liverpole (Monsignor) on Oct 09, 2007 at 14:30 UTC
    Hi g0n,

    For the record, it works fine with RedHat Linux v9, Perl version 5.8.0, and works equally well with Windows XP, ActiveState Perl version 5.8.8 (binary build 819).

    Could it be something as simple as needing $mw->update() at the end of your recompose subroutine?


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Tk Mainwindow resizing after child destroyed
by cmv (Chaplain) on Oct 09, 2007 at 14:34 UTC
    I cannot see the numbers after clicking on the button (I'm running on perl 5.8.8 under MacOSX).

    However, when I run it under windows xp on activestate perl (5.8.7 build813) it works fine.

    I wrestled with this issue a while ago, but can't quickly find the solution. I'll update this if I find it.

    -Craig
    UPDATE:
    Ahhh, zentara beats me to it...well done! His solution is what I ended up doing too. See, this proves that great minds, and little-teeny minds (me) sometimes think alike too.

Re: Tk Mainwindow resizing after child destroyed
by hangon (Deacon) on Oct 09, 2007 at 18:04 UTC

    It works fine on mine as well (ActiveState). Initializing the main window size may solve your problem.

    use strict; use warnings; use Tk; my $mw = MainWindow->new(); $mw->geometry('100x300'); ...