Control the fill : Layout « GUI « Perl






Control the fill

 

use Tk;
require Tk::BrowseEntry;

$numWidgets = 1;
my (@packdirs) = ();
my (@anchordirs) = ();
my (@fill) = ();
my (@expand) = ();

$mw = MainWindow->new(-title => "This is the title");

$f = $mw->Frame(-borderwidth => 1, 
                -relief => 'groove')
                ->pack(-side => 'top', 
                -fill => 'x');

$top = $mw->Toplevel(-title => "output window");
my $addbutton = $f->Button(-text => "Add Widget", 
                           -command => \&addwidget )->pack(-anchor => 'center');
foreach (0..$numWidgets) {
  my $b = $top->Button(-text => $_ . ": $packdirs[$_]")->pack;
  my %pinfo = $b->packInfo;
  $b->packForget;
  &addwidget($_);  
}
MainLoop;

sub repack {
  @w = $top->packSlaves;
  foreach (@w) { $_->packForget; }
  my $e = 0;
  foreach (@w)   {
    $_->configure(-text => "$e: $packdirs[$e]");
    $_->pack(-side => $packdirs[$e], 
             -fill => $fill[$e], 
             -expand => $expand[$e], 
             -anchor => $anchordirs[$e]);
    $e++;
  }
}

sub addwidget {
  my ($count) = @_;
  if (! defined $count)   {
    $numWidgets ++;
    $count = $numWidgets ;
  }
  
  $packdirs[$count] = 'top';
  $anchordirs[$count] = 'center';
  $fill[$count] = 'none';
  $expand[$count] = 0;
  
  my $f1 = $f->Frame->pack(-side => 'top', -expand => 1, 
    -fill =>'y', -before => $addbutton);

  $f1->BrowseEntry(-label => "-fill", -choices => [qw/none x y both/], 
    -variable => \$fill[$count], -browsecmd => \&repack)
    ->pack(-ipady => 5, -side => 'left');



  $top->Button(-text => $count . ": $packdirs[$count]",
    -font => "Courier 20 bold")->pack(-side => $packdirs[$count], 
    -fill => $fill[$count], -expand => $expand[$count]);
}

   
  








Related examples in the same category

1.Pack controls on a window
2.Pack left, right, both
3.Using pack to control widget placement
4.Using place method to set control location
5.Layout control: fill
6.Layout controls: Pack to the top
7.Pack Bottom
8.Pack Left
9.Pack Right
10.Pack Top
11.Adding radio buttom to Frame
12.Relayout(pack) the controls