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

Hi all, this is a sub in my program which create 1 row of 5 Text widgets (the sub can create up 100 rows) i want them to be display in a main windows and add a scorllbar so that i can scroll down and up between them
my $textTopStart = 228; my $textTop = $textTopStart; my $textHeight = 21; my @textWidth = ("4", "160", "309", "460", "609"); sub AddRow { $dude2 = $xscrollleft->Text(-font => $font, -background => 'white', +-foreground => 'black',-height => 1,-width => 22)->place( -x => $text +Width[0], -y => $textTop); $dude3 = $xscrollleft->Text(-font => $font, -background => 'white' +, -foreground => 'black',-height => 1,-width => 21)->place( -x => $te +xtWidth[1], -y => $textTop); $dude4 = $xscrollleft->Text(-font => $font, -background => 'white' +, -foreground => 'black',-height => 1,-width => 21)->place( -x => $te +xtWidth[2], -y => $textTop); $dude5 = $xscrollleft->Text(-font => $font, -background => 'white' +, -foreground => 'black',-height => 1,-width => 21)->place( -x => $te +xtWidth[3], -y => $textTop); $dude6 = $xscrollleft->Text(-font => $font, -background => 'white' +, -foreground => 'black',-height => 1,-width => 22)->place( -x => $te +xtWidth[4], -y => $textTop); $textTop += $textHeight; }

Replies are listed 'Best First'.
Re: Window scrollbar
by zentara (Cardinal) on Feb 02, 2009 at 20:06 UTC
    Make a Scrolled Pane. Into the Scrolled Pane, pack a Frame for each row. To each Frame, add your Text widgets, packing left to right. Here is the idea with a bunch of Scrolled Canvases. If you also need to control focus, it is shown too. Of course, for 100 rows, you will need to automate the loop, I just hardwired in 2 Frames. UPDATE: added -expand=>1,-fill=>'both' options to make resizing look nice.
    #!/usr/bin/perl use strict; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry('400x250'); my $mwf = $mw->Scrolled('Pane', -scrollbars=>'osoe', -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 %canv; for (0..4){ $canv{$_}{'obj'} = $f1->Scrolled('Canvas', -height => 100, -width => 100, -bg =>'white', -scrollbars => 'osoe', -scrollregion => [ 0, 0, 500, 500 ], )->pack(-side =>'left' ,-padx=>10,-pady=>10,-expand=>1,-fill=>'both +'); $canv{$_}{'obj'}->createText(50,50, -text => $_, -anchor => 'center', ); my $num = $_; $canv{$num}{'obj'}->CanvasBind( '<FocusOut>' => sub{ $canv{$num}{'obj'}->configure(-bg=>'lightyellow'); } ); $canv{$num}{'obj'}->CanvasBind( '<FocusIn>' => sub{ $canv{$num}{'obj'}->configure(-bg=>'lightgreen'); } ); } for (5..9){ $canv{$_}{'obj'} = $f2->Scrolled('Canvas', -height => 100, -width => 100, -bg =>'white', -scrollbars => 'osoe', -scrollregion => [ 0, 0, 500, 500 ], )->pack(-side =>'left', -padx=>10,-pady=>10 ,-expand=>1,-fill=>'bot +h'); $canv{$_}{'obj'}->createText(50,50, -text => $_, -anchor => 'center', ); my $num = $_; $canv{$num}{'obj'}->CanvasBind( '<FocusOut>' => sub{ $canv{$num}{'obj'}->configure(-bg=>'lightyellow'); } ); $canv{$num}{'obj'}->CanvasBind( '<FocusIn>' => sub{ $canv{$num}{'obj'}->configure(-bg=>'lightgreen'); } ); } my $button = $mw->Button(-text=>'Focus 1', -command => sub{ $canv{1}{'obj'}->focusForce; $canv{1}{'obj'}->configure(-bg=>'blue'); })->pack(); MainLoop();

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      I want it to look like the follow example main windows and row of 5 widgets just add scrollbar that can go up and down
      #!/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(); my $f2 = $mwf->Frame()->pack(); my $textTopStart = 328; my $textTop = $textTopStart; my $textHeight = 31; my @textWidth = ("4", "160", "309", "460", "609"); my $dude2 = $mw->Text(-background => 'white', -foreground => 'black' +,-height => 1,-width => 22)->place( -x => $textWidth [0], -y => $textTop); my $dude3 = $mw->Text(-background => 'white', -foreground => 'blac +k',-height => 1,-width => 21)->place( -x => $textWidth[1], -y => $textTop); my $dude4 = $mw->Text(-background => 'white', -foreground => 'blac +k',-height => 1,-width => 21)->place( -x => $textWidth[2], -y => $textTop); my $dude5 = $mw->Text(-background => 'white', -foreground => 'blac +k',-height => 1,-width => 21)->place( -x => $textWidth[3], -y => $textTop); my $dude6 = $mw->Text(-background => 'white', -foreground => 'blac +k',-height => 1,-width => 22)->place( -x => $textWidth[4], -y => $textTop); $textTop += $textHeight; MainLoop();
Re: Window scrollbar
by zentara (Cardinal) on Feb 02, 2009 at 20:45 UTC
    Don't mix, pack and place. Also, trying to maintain pixel positions with place is a losing proposition. Learn to use pack. This is how to do it.
    #!/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 Remember How Lucky You Are