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

I'm going gaga on a simple Perl/Tk operation here...

When -width is specified, why is the text label contents always centred? -justify is documented to only work with multi-line content (and I've tried it anyway and the content is still always centred). Experiments with pack, place, etc don't seem to shed any light on it.

How can the contents of a fixed-width text label be left-justified? The attached code shows that as soon as the widget is created, it's already centring the text, even before the button action changes the text content.

use strict; use Tk; my $mw = MainWindow->new; my $lbl = $mw->Label( # File -text => '(start)', # -width => 12, -background => 'bisque', # -justify => 'left', -relief => 'groove', )->pack; my $btn = $mw->Button( -text => "Go!", -command => \&test, )->pack; MainLoop; sub test { $lbl->configure(-text => 'A'); }
As usual, I'm trying this with ActiveState Perl, Build 813 under WinXP.

Any suggestions on where to look next?

Thanks.

Replies are listed 'Best First'.
Re: Content of Perl/Tk Fixed-Width Text Label is Always Centred!?
by strat (Canon) on Mar 29, 2006 at 07:36 UTC

    Maybe the pack-option -anchor does what you want...

    my $lbl = $mw->Label( # File -text => '(start)', # -width => 12, -background => 'bisque', # -justify => 'left', -relief => 'groove', ) ->pack(-side => 'top', -anchor => 'w');

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      Howdy!

      anchor is the option you want, but on the Label itself.

      yours,
      Michael
      Good try... but no cigar :)

      Your suggestion moves the label to the left side/top edge of the main window... but the initial content (and the re-configured content) is still in the centre of the fixed-width label.

      Fanx! ...but still researching...

Re: Content of Perl/Tk Fixed-Width Text Label is Always Centred!?
by thundergnat (Deacon) on Mar 29, 2006 at 11:59 UTC

    strat had the right idea but the wrong location. -anchor isn't only a pack option, it's a standard widget option. Try:

    use strict; use Tk; my $mw = MainWindow->new; my $lbl = $mw->Label( # File -text => '(start)', -width => 12, -background => 'bisque', -relief => 'groove', -anchor => 'w', )->pack; my $btn = $mw->Button( -text => "Go!", -command => \&test, )->pack; MainLoop; sub test { $lbl->configure(-text => 'A'); }
      Bingo! The -anchor on the widget works a treat. Docs on Tk::options was the clue.

      Some experimenting now seems to say you can even use it in whatever->configure() to change the "justification" on the fly.

      There's always another subtlety to discover with Perl/Tk :)

      Many thanks, once again, dear monks.

Re: Content of Perl/Tk Fixed-Width Text Label is Always Centred!?
by zentara (Cardinal) on Mar 29, 2006 at 12:09 UTC
    strat almost had it...... the -anchor=>'w' option belongs in the label options, not the packing options.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; my $lbl = $mw->Label( # File -text => '(start)', -width => 12, -background => 'bisque', -justify => 'left', -anchor =>'w', -relief => 'groove', )->pack; my $btn = $mw->Button( -text => "Go!", -command => \&test, )->pack; MainLoop; sub test { $lbl->configure(-text => 'A'); }

    I'm not really a human, but I play one on earth. flash japh