http://www.perlmonks.org?node_id=991959

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

I'd like to add a scrollbar to this frame. I can't seem to figure out how to get this to work properly. Here is an example of one attempt.

#!/usr/bin/perl use warnings; use strict; use Tk; use Cwd; ###################################################################### +##################### # GUI Building ###################################################################### +##################### # make the selections available to the print_input_values sub my %t; my %mat; my %ort; my %mat_lab; my $scr; # Create Main Window my $mw = new MainWindow; my $lam_num; my $row=1; my $column=1; my $lam_mat_frm = $mw -> Frame(); $lam_mat_frm->grid(-row=>$row, -column=>$column,-columnspan=>6); my $lam_num_lab = $lam_mat_frm -> Label(-text=>"Input the number of pl +ies in the laminate.", -font=>"ansi 10 bold"); $lam_num_lab->grid(-row=>$row, -column=>$column); $column++; my $lam_num_ent = $lam_mat_frm -> Entry(-textvariable=> \$lam_num); $lam_num_ent->grid(-row=>$row, -column=>$column); $column++; my $lam_data_button = $lam_mat_frm->Button(-text=>"Input Laminate Data +", -command=> \&input_lam_data); $lam_data_button->grid(-row=>$row, -column=>$column); $column++; my $reset_button = $lam_mat_frm->Button(-text=>"Reset", -command=> \&do_reset); $reset_button->grid(-row=>$row, -column=>$column); $row++; my $lam_frm = $mw -> Frame(); $lam_frm->grid(-row=>$row,-column=>$column,-columnspan=>6); MainLoop; sub do_reset { $lam_num_ent->delete(0,'end'); $lam_frm->packForget; $row=2; $column=4; $lam_frm = $mw -> Frame(); $lam_frm->grid(-row=>$row,-column=>$column,-columnspan=>6); } sub input_lam_data { $row=4; $column=2; my $scrollbar = $lam_frm->Scrollbar(); $scrollbar->grid(-row => 1, -column => 5); my @lam_mat_t_lab = ( 'Material', 'Thickness', 'Orientation', ); my @mats = ("PW","8HS","Tape"); my %mat_optmen; my $n = 1; $row=5; $column=2; do { $mat_optmen{$n} = $lam_frm -> Optionmenu(-options => \@mats, -variable => \$mat{$n}); $mat_optmen{$n}->configure(-yscrollcommand=>['set',$scrollbar] +); $mat_optmen{$n}->grid(-row=>$row,-column=>$column); $scrollbar->configure(-command=>['yview'=>$mat_optmen{$n}]); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); }

Replies are listed 'Best First'.
Re: Scrollbar in Frame
by zentara (Archbishop) on Sep 06, 2012 at 10:17 UTC
    I don't know how many times you have posted this code for us to work on it for you, maybe 3? :-)

    Which frame are you trying to put scrollbars on? The mainwindow, or just on the popup frame which appears below? Or, do want the entire thing scrolled?

    As mentioned above, you can use a Scrolled('Frame') or my favorite Scrolled('Pane'), to make your widgets scrolled.

    If you want avoid using Tk::Scrolled, with it's automatic scrollbars added, this is how you would manually add a scrollbar to a widget. A basic example.

    #!/usr/bin/perl use warnings; use diagnostics; use Tk; $top = MainWindow->new(); $car_list = $top->Listbox(-width => 15, -height => 4, )->pack(-side => 'left', -padx => 10); $car_list->insert('end', # Insert at end, the following list "Acura", "BMW", "Ferrari", "Lotus", "Maserati", "Lamborghini", "Chevrolet" ); # Create scrollbar, and inform it about the listbox $scroll = $top->Scrollbar(-orient => 'vertical', -width => 10, -command => ['yview', $car_list] )->pack(-side => 'left', -fill => 'y', -padx => 10); # Inform listbox about the scrollbar $car_list->configure(-yscrollcommand => ['set', $scroll]); MainLoop();

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Scrollbar in Frame
by Anonymous Monk on Sep 05, 2012 at 23:39 UTC