Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^9: resizing problem with Tk appl using PackForget

by zentara (Archbishop)
on Dec 08, 2011 at 12:18 UTC ( #942409=note: print w/replies, xml ) Need Help??

in reply to Re^8: resizing problem with Tk appl using PackForget
in thread resizing problem with Tk appl using PackForget

Still, if I'm honest, I think this is a limitation of Tk: imho their should be more automatic resize options than me coding and remembering the size of all frames.

Well, I think it maybe a widget layout design problem on your part, which probably can be easily fixed by carefully allowing all the child widgets to repack themselves, according to the new size.

If you want to remove a frameful of widgets, you should remove it's child widgets first with packForget. (Its like you can't remove a directory which still has subdirs). In other words, don't use complete Frame removal to remove a set of widgets, remove each widget itself, and leave the Frame. Then when you do a resize, and want to repack your frame, each widget gets repacked, and will do so according to the new reported size of the Frame they are being packed into.

I hope that made sense. Play with the following code, it creates new widgets on a rebuild, but you could repack your hidden widgets instead. A second example is also provided.

#!/usr/bin/perl use warnings; use strict; use Tk; my $top = new MainWindow; my @counts = ('a'..'z'); my %cbuttons; my $frame = $top->Frame()->pack(); setup_page(); $top->Button(-text => "packForget", -command => sub{ my @w = $frame->packSlaves; foreach (@w) { $_->packForget; } })->pack(); $top->Button(-text => "repack", -command => sub{ &setup_page })->pack(); $top->Button(-text => "Exit", -command => sub {exit})->pack; MainLoop; sub setup_page{ for (1..4){ my $text = shift @counts; $cbuttons{$_}{'cb'} = $frame->Checkbutton( -text => $text, -variable => \$cbuttons{$_}{'val'}, -command => \&SetState, )->pack; } }
A second example:
#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; my $vh = $mw->vrootheight; my $vw = $mw->vrootwidth; $mw->geometry($vw.'x'.$vh.'+0+0'); $mw->overrideredirect(1); #grabs full control $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size=>int(-18*18/14)); my $topframe = $mw->Frame->pack(); my $frame = $mw->Frame->pack(); my $txt; #make these globals so you reuse them my $canvas; #and avoid memory gains of making too many #redundant widgets my $count = 0; # instead of a button you could make navigation arrows $topframe->Button(-text => "Change Screen", -command => sub{ #clean out frame's children my @w = $frame->packSlaves; foreach (@w) { $_->packForget; } #clean out frame $frame->packForget; $count++; &build($count); })->pack(-side=>'left',-padx=>20); $topframe->Button(-text => "Exit", -command => sub {exit})->pack(-side=>'right',-padx=>20); &build(0); #setup first screen MainLoop; sub build { my $count = shift; $txt = $frame->Text(-bg=>'white',-font=>'big',-height=>5)->pack(-fill +=>'x'); $txt->insert('end',"\t\t\t\t Page $count"); # Note that the 'virtual window' height and width are $vh and $vw # respectively, so we use those dimensions for our Canvas height # and width, and let the Canvas expand and fill in both x and y # directions. # $canvas = $frame->Canvas( -width => $vw, -height => $vh, -background =>'blue', )->pack(-expand => 1, -fill => 'both'); $canvas->createRectangle(80, 80, 200, 200, -fill => 'yellow'); $canvas->createText(125, 125, -fill => 'black', -text => "page $count", -font => 'big', ); $frame->pack(); #reshow it $mw->update; }

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://942409]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2017-01-23 23:55 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (199 votes). Check out past polls.