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

hi everybody my problem is iam dsplaying button widgets dynamically in the main window. the number of buttons is decided during the runtime so if the number of buttons exceeds the window size some buttons are hidden so i need to use a scroll bar can any body suggest how to scroll the mainwindow with widgets packed in it.

Replies are listed 'Best First'.
Re: how to scroll a mainwindow in TK
by thundergnat (Deacon) on Aug 15, 2005 at 13:58 UTC

    Another option is to pack the buttons into a Pane inside the main window. A benefit to this is you can make just a portion of the window scrollable if you should so desire.

    use warnings; use strict; use Tk; use Tk::Pane; my $top = MainWindow->new; my $pane = $top->Scrolled('Pane', -scrollbars => 'e', )->pack(-expand => 'y', -fill => 'both'); for (0 .. 9) { $pane->Button( -text => $_, -width => 15, -command => [sub{print "Button $_[0] Pressed\n"},$_], )->pack; } $top->Button( -text => 'Quit', -command => sub{$top->destroy}, )->pack; MainLoop;
Re: how to scroll a mainwindow in TK
by rinceWind (Monsignor) on Aug 15, 2005 at 12:59 UTC

    Are you sure this is what you want? I have Tk apps that have dynamic buttons, but they tend to resize their toplevel window as more buttons are added.

    I also use stretchy grids sometimes where the size of the grid cells is a proportion of the toplevel window's size.

    Anyway, coming back to your original request, this can be done using a Scrolled HList (an HList is an easy way to get a vertical array of buttons capable of being scrolled).

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: how to scroll a mainwindow in TK
by zentara (Cardinal) on Aug 15, 2005 at 17:35 UTC
    In addition to what thundergnat has shown, you can make the scrollbars appear 'O'nly when you need them with the 'o' notation.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; my $top = MainWindow->new; $top->geometry("150x300"); my $pane = $top->Scrolled('Pane', -scrollbars => 'oe', # 'osoe' for both bars )->pack(-expand => 'y', -fill => 'both'); for (0 .. 5) { $pane->Button( -text => $_, -width => 15, -command => [sub{print "Button $_[0] Pressed\n"},$_], )->pack; } $top->Button( -text => 'Quit', -command => sub{$top->destroy}, )->pack; MainLoop;

    I'm not really a human, but I play one on earth. flash japh
      zentar's example was a great Thank You - Im trying to get this to work using "place()" rather than "pack()" ?