Tk geometry management

From JmPm

Jump to: navigation, search

Place (by coordinates):

   use Tk;
   my $mw = new MainWindow;
   $mw->title( 'Place by coordinates' );
   my $l1 = $mw->Label(-text => '50 pixels down',-relief=>'groove')->place(-x => 0, -y => 50);
   # relative - 0.0 to 1.0  (0.5 = in the middle of the screen).
   my $l1 = $mw->Label(-text => '10% down and right',-relief=>'groove')->place(-relx => 0.1, -rely => 0.1);
   # in the middle but some pixels to the right:
   my $l1 = $mw->Label(-text => '10 pixels from the middle',-relief=>'groove')->place(-relx => 0.5, -x => 10, -y => 90);
   my $l1 = $mw->Label(-text => '20 pixels from the middle',-relief=>'groove')->place(-relx => 0.5, -x => 20, -y => 90);
   MainLoop;

Pack:

   use Tk;
   my $mw = new MainWindow;
   $mw->title( 'left_frame' );
   my(@pl) = qw/-side left -expand yes -padx 30 -pady 30 -fill both/;
   my $left_frame = $mw->Frame->pack(@pl);
     $left_frame->Label(-text => '1',-relief=>'groove')->pack('-fill','both');
     $left_frame->Label(-text => '2',-relief=>'groove')->pack('-fill','both');
     $left_frame->Label(-text => '3',-relief=>'groove')->pack('-fill','both');
     $left_frame->Label(-text => '4',-relief=>'groove')->pack('-fill','both');
   (@pl) = qw/-side right -expand yes -padx 1 -pady 1 -fill both/;
   my $right_frame = $mw->Frame->pack(@pl);
     $right_frame->Label(-text => '1',-relief=>'groove')->pack('-fill','both');
     $right_frame->Label(-text => '2',-relief=>'groove')->pack('-fill','both');
     $right_frame->Label(-text => '3',-relief=>'groove')->pack('-fill','both');
     $right_frame->Label(-text => '4',-relief=>'groove')->pack('-fill'=>'both');
   MainLoop;

Grid (like HTML tables):

use Tk;
# Main Window
my $mw = new MainWindow;
#Text Area
my $txt = $mw -> Text(-width=>40, -height=>10);
my $srl_y = $mw -> Scrollbar(-orient=>'v',-command=>[yview => $txt]);
my $srl_x = $mw -> Scrollbar(-orient=>'h',-command=>[xview => $txt]);
$txt -> configure(-yscrollcommand=>['set', $srl_y], -xscrollcommand=>['set',$srl_x]);
#Geometry Management
$txt -> grid(-row=>1,-column=>1);
$srl_y -> grid(-row=>1,-column=>2,-sticky=>"ns");
$srl_x -> grid(-row=>2,-column=>1,-sticky=>"ew");
MainLoop;

Form (with percentages):

use Tk;
my $Main = MainWindow->new();
my $box4 = $Main->Label(-text => 'box4', -borderwidth => 1, -relief => "raised");
my $box1 = $Main->Label(-text => 'box1',-borderwidth => 1, -relief => "raised");
my $box2 = $Main->Label(-text => 'box2',-borderwidth => 1, -relief => "raised");
my $box3 = $Main->Label(-text => 'box3',-borderwidth => 1, -relief => "raised");
$box1->form(-top => '%0', -left => '%10', -right => '%90');
$box2->form(-top => $box1, -left => '%0', -right => '%50', -bottom =>$box4);
$box3->form(-top => $box1, -left => $box2, -right => '%100', -bottom =>$box4);
$box4->form(-left => '%0', -right => '%100', -bottom => '%100');
MainLoop;
Personal tools