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

I'm having a difficult time getting the 'Exit' button to be on the left-hand side of its frame rather than at the center. Anyone have suggestions? Thanks.
#!/usr/bin/perl -w use strict; use Tk; my $window_title = 'window title'; my $overview_message = "A message that is fairly long for this window\n"; my $mw = MainWindow->new; $mw->title( $window_title ); my $fA = $mw->Frame->pack(-side => 'bottom', ); $fA->Button( -text => 'Exit', -command => sub { exit; }, )->pack( -fill => 'both', -side => 'left', -anchor => 'w', ); my $fB = $mw->Frame->pack(-side => 'top'); my $user_font_choice = 'arial 10 bold'; my @listbox_items = split /\n/, $overview_message; my $listbox = $fB->Scrolled( "Listbox", -scrollbars => 'onoe', # 'onoe': optional north, east -font => $user_font_choice, -selectmode => 'single', -height => scalar @listbox_items, -width => 0, )->pack( -side => 'top', -fill => 'both', -expand => 1 ); $listbox->insert( 'end', @listbox_items ); MainLoop();

Replies are listed 'Best First'.
Re: Get a Tk Button to align left in a frame
by shmem (Chancellor) on May 06, 2007 at 22:00 UTC

    The containing frame must fill 'x':

    my $fA = $mw->Frame->pack(-side => 'bottom', -fill => 'x');

    In such cases it is helpful to construct the container with a different background colour (or with a border) to see its limits.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Get a Tk Button to align left in a frame
by ff (Hermit) on May 06, 2007 at 23:21 UTC
    Thanks! That's working for my main application.

    Now, when I try your suggestion of using a background color to see this better, I get strange results. When the -fill => 'x' is "on", I get the purple background, as in

    my $fA = $mw->Frame( -background => 'purple', )->pack( -side => 'bottom', -fill => 'x', );

    But when -fill => 'x' is disabled, as in

    my $fA = $mw->Frame( -background => 'purple', )->pack( -side => 'bottom', #-fill => 'x', );

    there's not a trace of purple. Is that because the frame is only as big as the button unless you manipulate it via something like -fill?

      Is that because the frame is only as big as the button unless you manipulate it via something like -fill?
      Yes. You can see that effect if you also set -highlightbackground and -highlightthickness:
      my $fA = $mw->Frame( -background => 'purple', -highlightbackground => 'maroon', -highlightthickness => '1', )->pack( -side => 'bottom', #-fill => 'x', );

      You will see a maroon border around the button - but that's the border of the frame. If you cycle the elements of that window with <Tab>, you will see that the black line around the button is 2 pixels thick - that is because the frame and the button are highlighted, visible if you add a -highlightcolor to your frame, of if you let it fill in x direction.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}