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

All kinds of fun things happen when I dynamically rebuild my GUI in Tk. Here's an example: I have an array of recent commands (as per the Hash of Arrays example I shared the other day).

I display a button panel that has a button for each entry in the array which results from sorting that hash. Various factors cause the number of buttons to change.
... for (my $i = 0; $i <= $#$rrecent; $i++) { $$rbut[$i] = $bpanelframe->Button( -text => $$rrecent[$i], -fg => $clrfgbut, + -activeforeground => $clrfgbut, -command => [\&fire, $$rrecent[$i], $_[0]] ) ->pack( -side => 'top', -anchor => 'n', -fill => 'x', -expand => 1 );} } ... $bpanelframe->pack( -side => 'top', -anchor => 'n', -fill => 'both', -expand => 1 );
My problem is that $bpanelframe does not resize to accomodate the number of buttons inside it. Before calling this code the previous panel's $bpanelframe was ->packForget-ed. I either get blank space between the buttons or some buttons are neatly hidden underneath the frame packed below it.

The behaviour is inconsistent; sometimes it does resize, and other times it doesn't. Do I need to packForget all the way out to my outermost frames, or is there something else I'm missing? I've checked numerous tutorials on the Web and reconfiguring on the fly is not something that's discussed. It almost works right; that's the maddening part! :D

Replies are listed 'Best First'.
Re: dynamic resizing in Tk
by zentara (Cardinal) on Jan 17, 2005 at 21:18 UTC
    You are asking a fairly complex question, without showing any working example to demonstrate the problem. It sounds like you are thinking right in needing to repack the whole program. You might see if withdrawing the mainwindow, and raising it again, will force a redraw. But generally, you should try to keep your buttons in a separate frame, then you can packForget the whole frame, rebuild it with new buttons, then pack it again into the $mw. Or use a separate $toplevel, which you can rebuild out of site, without messing with your $mw.

    I'm not really a human, but I play one on earth. flash japh
      That is what I'm doing. Sorry I didn't include more code; it's a very complex app with many code paths and a lot of symbolic references. All the internal buttons are packForgotten, then $bpanelframe is packForgotten, then the (new) buttons are packed and $bpanelframe is re-packed.

      Thanks for the confirmation that I need to try going all the way out in order to get consistent behaviour.
        Thanks for the confirmation that I need to try going all the way out in order to get consistent behaviour.

        Thats why it is easier to use a separate toplevel window, which you can rebuild out-of-sight.


        I'm not really a human, but I play one on earth. flash japh