liverpole has asked for the wisdom of the Perl Monks concerning the following question:
This is a Tk problem I've tried to find an answer to for some time, and I'm finally posting it here in case anyone knows what I'm missing (whether it be an actual bug in Tk, or just something that's been overlooked).
When I create a non-scrolled Tk frame and pack a new frame into the top of it (with -expand => 1 and -fill => 'x'), it behaves as I would expect.
When I create a similar Tk frame using the "Scrolled" method, though, the newly packed frame inside it doesn't pack to the top of the frame (it stays in the center), and the -expand and -fill options don't seem to work correctly.
The original program is quite large, but this test program shows the same behavior:
#!/usr/bin/perl -w # Strict use strict; use warnings; # Libraries use Tk; use Tk::Pane; use Tk::NoteBook; # Main Program # # Create the GUI, and the top frame my $mw = new MainWindow(); my $top = make_frame($mw, 'white', 'noscroll', 'top', 1, 'both'); # Notebook and notebook pages my $nb = $top->NoteBook()->pack(-expand => 1, -fill => 'both'); my $pg1 = $nb->add('page1', -label => 'Unscrolled'); my $pg2 = $nb->add('page2', -label => 'Scrolled'); # Create subframes (scrolled vs. non-scrolled) my $f1 = make_frame($pg1, 'green', 'noscroll', 'top', 0, 'both'); my $f2 = make_frame($pg2, 'blue', 'scrolled', 'top', 0, 'both'); # Populate frames with subframe containing a label and a button. # Attempt to pack the subframe into the "top" of the containing widget +. label_button_frame($f1, 'gold'); # Packs as expected label_button_frame($f2, 'red'); # Fails to pack nicely?? # Manage gui MainLoop; # Subroutines sub make_frame { my ($parent, $bgcolor, $scrolled, $side, $expand, $fill) = @_; my $b_scr = ($scrolled eq 'scrolled')? 1: 0; my $frame = $b_scr? $parent->Scrolled('Frame'): $parent->Frame(); $frame->configure(-bg => $bgcolor); $frame->pack(-side => $side, -expand => $expand, -fill => $fill); return $frame; } sub label_button_frame { my ($parent, $bgcolor) = @_; my $mw = $parent->toplevel(); my $fr = make_frame($parent, $bgcolor, 'noscroll', 'top', 1, 'x'); $fr->configure(-relief => 'groove', -borderwidth => 4); $fr->Label(-text => 'My Label', -rel => 'groove')->pack(-side => ' +left'); my $pcmd = sub { $mw->destroy }; $fr->Button(-text => 'Exit', -command => $pcmd)->pack(-side => 'ri +ght'); return $fr; }
Running the program shows a notebook with two tabs, labeled "Unscrolled" and "Scrolled". The "Unscrolled" page shows a yellow frame, packed to the top of its parent frame, with a label "My label" packed to the left, and the "Exit" button packed to the right.
On the "Scrolled" page, I'd like to understand why the red frame doesn't look similar with respect to its position relative to its parent frame, and failure to -expand and -fill in the X direction.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Undesired behavior using the Scrolled method in Tk
by zentara (Cardinal) on Dec 29, 2006 at 22:12 UTC |