Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

tk positioning of widgets

by gibsonca (Beadle)
on Oct 22, 2012 at 19:13 UTC ( [id://1000406]=perlquestion: print w/replies, xml ) Need Help??

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

I'm having trouble with positioning my tk text/input boxes. I'd like them to be either top, left justified, or bottom left justified. I have tried -side <options> The example source shows, among other things, I don't know how to do this. Thanks.

#!/usr/bin/perl use strict; use warnings; use Env; use integer; use File::Find; use feature ":5.10"; use Data::Dumper; use Tk; use Tk::ProgressBar; use Tk::BrowseEntry; use Text::CSV_XS; use Net::FTP; my( $cm21_info_frame, $exit, $g_rfs_Version, $gCm21Dir, $gDir, $gDirectory, $gLoad, $gPcDir, $gOutDir, $gProject, $gUserFile, $help, $mbar, $mw, $pc_info_frame, $txt, ); # MAIN $g_rfs_Version = "debugging"; $gDir = "\\tmp\\abc"; # Main Window $mw = new MainWindow; $mw->geometry("900x400"); $mw->title("MENU DEBUG V +ersion $g_rfs_Version"); &selectProject($mw); &selectLoad($mw); my $main_menu = $mw->Menu(); $mw->configure(-menu => $main_menu); # see if there is a persistence directory to use. $mbar = $mw -> Menu(); $mw -> configure(-menu => $mbar); # The Main Buttons my $file = $mbar -> cascade(-label => "Commands", -underl +ine => 0, -tearoff => 0); my $compile = $mbar -> cascade(-label => "Compile", -underl +ine => 0, -tearoff => 0); my $generate = $mbar -> cascade(-label => "Save Results", -underl +ine => 0, -tearoff => 0); $exit = $mbar -> cascade(-label => "Exit", -underl +ine => 0, -tearoff => 0); $help = $mbar -> cascade(-label => "Help", -underl +ine => 0, -tearoff => 0); ## File Menu ## $file -> checkbutton(-label =>"Select Program", -underline => 0, -command => [\&selectProgram, "Select program"]); $file -> checkbutton(-label =>"Select Directory", -underline => 0, -command => [\&selectDirectory, "Select Directory"]); $file -> checkbutton(-label =>"Select Function", -underline => 0, -command => [\&selectFunction, "Select Function"]); $file -> separator(); # ------------------------------------------ $file -> command(-label =>"Exit", -underline => 1, -command => sub + { exit } ); ### TEXT AREA $txt = $mw -> Scrolled('Text',-width => 130,-scrollbars=>'e') -> pac +k (); $txt->insert('end',"\n Working directory is $gDir"); # CREATE VIEW FRAME my $info_frame = $mw->Frame( -relief => 'ridge', -borderwidth => '1' )->pack(-side => 'bottom', -fill => 'x'); # PC DIRECTORY FRAME $pc_info_frame = $mw->Frame->pack( -side => "bottom", -fill => 'x'); + # Label $pc_info_frame->Label( -text => "PC Directory : " )->pack( -side => "left" ); # Input field $pc_info_frame->Entry( -width => 120, -textvariable => \$gPcDir )->pack( -side => "left" ); ## Compile ## $compile -> command(-label =>"compile test files", -command => sub + { compile(); } ); ## Save Results ## $generate -> command(-label =>"to SUM file", -command => sub { go( +); } ); ## Exit ## $exit -> command(-label =>"out of here", -command => sub { exit(0) +; } ); ## Help ## $help -> command(-label =>"About", -command => sub { $txt->delete('1.0','end'); $txt->insert('end', "About rfs.pl --------------------------------------- Version $g_rfs_Version --------------------------------------- Purpose: 'Commay Author: Website : http://www E-Mail : \n"); }); MainLoop; # ------------------------------------------------- sub selectProject { # pick a project # ------------------------------------------------- my $mother = shift; # Create dropdown and another element which shows my selection my $dropdown_value = "aaa"; my $dropdown = $mother->BrowseEntry( -label => "Project:", -variable => \$dropdown_value, )->pack(-side => "left"); my $showlabel = $mother->Label( -text => "Project: $dropdown_value", )->pack(-side => "left"); # Configure dropdown $dropdown->configure( # What to do when an entry is selected -browsecmd => sub { $showlabel->configure(-text => "Project: $dropdown_value" +), }, ); # Populate dropdown with values foreach ( qw/aaa bbb ccc/ ) { $dropdown->insert('end', $_); } # Set the initial value for the dropdown $dropdown_value = "bbb"; } # end of selectProject() # ------------------------------------------------- sub selectLoad { # pick a load # ------------------------------------------------- my $mother = shift; # Create dropdown and another element which shows my selection my $dropdown_value = "2.1"; my $dropdown = $mother->BrowseEntry( -label => "Load:", -variable => \$dropdown_value, )->pack(-side => "left"); my $showlabel = $mother->Label( -text => "Load: $dropdown_value", )->pack(-side => "left"); # Configure dropdown $dropdown->configure( # What to do when an entry is selected -browsecmd => sub { $showlabel->configure(-text => "Load: $dropdown_value" ), }, ); # Populate dropdown with values foreach ( qw/1.0 1.1 2.0 2.1/ ) { $dropdown->insert('end', $_); } # Set the initial value for the dropdown $dropdown_value = "2.1"; } # end of selectLoad() # ----------------- sub selectDirectory { # ----------------- $gDir = $mw->chooseDirectory; $gDir =~ s/\//\\/g; $gOutDir = $gDir . "\\Converted"; $txt->insert('end',"\n\nNew directory is $gDir"); $gDirectory = $gDir; opendir(DIR, $gDirectory) or die "couldn't open $gDirectory: $!\ +n"; my @files = grep(/\.*$/, readdir(DIR)); closedir DIR; if ($#files < 3) { $txt->insert('end',"\n\n*** ERROR 1 *** \n"); $txt->insert('end',"Directory, $gDirectory, is empty! \n\n"); $mw->update; } } # end of selectDirectory() # ----------------- sub selectProgram { # ----------------- $txt->insert('end',"\n\nSelect Program is TBD"); $mw->update; } # end of selectProgram() # ----------------- sub selectFunction { # ----------------- $txt->insert('end',"\n\nSelect Function is TBD"); $mw->update; } # end of selectFunction() # ----------------- sub compile { # ----------------- $txt->insert('end',"\n\nCompiling function is TBD"); $mw->update; } # end of compile() # --------- sub go { # --------- &OnGenerate(); } # end of go() # of main() ###

Replies are listed 'Best First'.
Re: tk positioning of widgets
by zentara (Archbishop) on Oct 23, 2012 at 10:27 UTC
    In your example code, you didn't say where you wanted the Text widget to actually be. Normally when designing, when you want custom packing, you create a Frame (with it options properly set to expand) and pack your Text widget into the Frame. You can't just pack stuff into Frames and expect the layout to work. Most of the time you need nested Frames.

    Here is a simple example demonstrating the idea.

    #!/usr/bin/perl use strict; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry( '400x250' ); my $mwf = $mw->Scrolled( 'Pane', -scrollbars => 'ose', -sticky => 'nwse', )->pack( -expand => 1, -fill => 'both' ); my $f1 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); my $f2 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); my %dudes; my $dude_count = 1; foreach my $fr($f1, $f2){ foreach (1..6){ $dudes{$dude_count}{'text'} = $fr->Text( -background => 'white', -foreground => 'black', -height => 10, -width => 22 )->pack(-side=>'left', -expand => 1, -fill => 'both' ); $dudes{$dude_count}{'text'}->insert('end', "\n Dude $dude_count\n") +; $dude_count++; } } MainLoop();

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

      OK, I guess a picture is worth apx 1,000 words: What I can do is the following:

      ---------------------------------- | 400x500 (write only) text area | ---------------------------------- Label1: drop down (user's input echo'd from Label1) Label2: drop down (user's input echo'd from Label2) Select Directory (Text are with user's input dir echo'd) Label3: drop down (User's input echo'd from Label3) : :

      What I can not do is put anything on the same line, so as to save space. What does work is letting everything either sorta left justify or default to centered. And I can live with that, but I was hoping to make it look a bit more concise or 'clean'. Thanks!

        A program is worth a million words. :-)

        Here is a simple example. For very complex layouts, you can even have subframes within each horizontal frame. For instance when you want very hard to obtain alignments. Here are 2 examples which show the ropes.

        #!/usr/bin/perl use strict; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry( '1000x600' ); my $mwf = $mw->Scrolled( 'Pane', -scrollbars => 'ose', -sticky => 'nwse', )->pack( -expand => 1, -fill => 'both' ); # make some frames to contain items on same line my $f1 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); my $f2 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); my $f3 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); my $f4 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); my $f5 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); # ---------------------------------- # | 400x500 (write only) text area | # ---------------------------------- # Label1: drop down # (user's input echo'd from Label1) # Label2: drop down # (user's input echo'd from Label2) # Select Directory # (Text are with user's input dir echo'd) # Label3: drop down # (User's input echo'd from Label3) my $text1 = $f1->Text(-bg => 'white')->pack(-side => 'left'); my $label1 = $f2->Label(-bg => 'lightseagreen', -text => 'left') ->pack( -side => 'left'); my $label2 = $f2->Label(-bg => 'lightyellow', -text => 'right') ->pack( -side => 'right'); my $text2 = $f3->Text(-bg => 'black')->pack(-side => 'right'); my $label3 = $f4->Label(-bg => 'hotpink', -text => 'bottom') ->pack(); MainLoop();
        and Label alignment
        #!/usr/bin/perl use Tk; use strict; use warnings; my $mw = MainWindow->new( -bg => 'beige', ); my $l = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#EEE000', )->pack( -fill => 'x', -side => 'left', -anchor => 'n', ); my $r = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#F0F000', )->pack( -fill => 'both', -side => 'left', -anchor => 'n', ); my $r2 = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#F0F000', )->pack( -fill => 'both', -side => 'left', -anchor => 'n', ); for ( qw[Name: Version: Description: Date: ] ){ $l->Label( -text => $_, -justify => 'left', -anchor => 'w', )->pack( #-anchor => 'e', # if you don't fill -fill => 'x', ); $r->Label( -text => $_, # -justify => 'right', -anchor => 'e', )->pack( -anchor => 'e', # if you don't fill #-fill => 'x', ); $r2->Label( -text => $_, -justify => 'right', # -anchor => 'e', )->pack( #-anchor => 'e', # if you don't fill #-fill => 'x', ); } #$top->Label(-text => "Enter the scroll frame")->pack; MainLoop;

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

        OK, I guess a picture is worth apx 1,000 words

        :D So first, words which don't paint a picture, and now, a picture of what you're able to accomplish by yourself?

        before (multi line)  perl -MTk  -MTk::WidgetDump  -e " $mw = tkinit; for(0..3){ $f = $mw->Frame(-bg => (qw/ red green blue orange /)[$_] )->pack(qw/-expand 1 -fill both /); $l = $f->Label(-text => qq/T$_/ ); $t = $f->Entry(-text => qq/E$_/); $_->pack(qw/-anchor nw /) for $l, $t; } $mw->WidgetDump; MainLoop "

        after (same line)  perl -MTk  -MTk::WidgetDump  -e " $mw = tkinit; for(0..3){ $f = $mw->Frame(-bg => (qw/ red green blue orange /)[$_] )->pack(qw/-expand 1 -fill both /); $l = $f->Label(-text => qq/T$_/ ); $t = $f->Entry(-text => qq/E$_/); $_->pack(qw/-anchor nw -side left /) for $l, $t; } $mw->WidgetDump; MainLoop "

        after (same line, one stretchy )  perl -MTk  -MTk::WidgetDump  -e " $mw = tkinit; for(0..3){ $f = $mw->Frame(-bg => (qw/ red green blue orange /)[$_] )->pack(qw/-expand 1 -fill both /); $l = $f->Label(-text => qq/T$_/ ); $t = $f->Entry(-text => qq/E$_/); $l->pack(qw/-anchor nw -side left /); $t->pack(qw/-anchor nw -side left -fill x -expand 1 /); } $mw->WidgetDump; MainLoop "

        grid alternative (grid is tricky ) perl -MTk  -MTk::WidgetDump  -e " $mw = tkinit; $f = $mw->Frame(qw/-bg red/)->pack(qw/-expand 1 -fill both /); for(0..3){ $l = $f->Label(-text => qq/T$_/ ); $t = $f->Entry(-text => qq/E$_/); $l->grid(-row => $_, qw/ -column 1 /); $t->grid(-row => $_, qw/ -column 2 -sticky news /);  } $f->Label()->grid(qw/ -row 4 -column 2 -sticky news /);  $f->gridColumnconfigure ( 2, -weight=>1); $f->gridRowconfigure ( 4, -weight=>1); $mw->WidgetDump; MainLoop "

Re: tk positioning of widgets
by kcott (Archbishop) on Oct 23, 2012 at 05:31 UTC

    G'day gibsonca,

    The documentation for individual widgets (e.g. Tk::Entry) lists Standard Options (see Tk::options for details) and Widget-Specific Options. Depending on the desired layout of your GUI, choose appropriate options such as -anchor, -justify and -orient.

    Geometry managers (e.g. Tk::pack which you are using in your code) also have options which affect layout.

    -- Ken

Re: tk positioning of widgets
by Anonymous Monk on Oct 23, 2012 at 01:26 UTC

    So you're trying to position some boxes? How? Draw me an ascii picture man?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1000406]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found