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

I am trying to get the text in a label nicely spaced to line up with other parts of the GUI. I have tried using the space character to do this.
However, Tk seems to concatenate consecutive spaces so that I get jut one space between adjacent words.
Is there a way to overcome this or some other means of laying out the text in a label to get what I want?

Replies are listed 'Best First'.
Re: Perl Tk Spaces in Text of Labels
by zentara (Cardinal) on Jul 02, 2008 at 12:41 UTC
    An example would be nice, it's hard to say what you mean exactly. Are you trying to align vertically or make a nice grid? This shows one way using pack. I think you are looking for the justify and anchor option. Also look at -padx.
    #!/usr/bin/perl use Tk; use strict; use warnings; my $mw = MainWindow->new( -bg => 'beige', ); my $l = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#EEE000', )->pack( -fill => 'x', -side => 'left', -anchor => 'n', ); my $r = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#F0F000', )->pack( -fill => 'both', -side => 'left', -anchor => 'n', ); my $r2 = $mw->Frame( -width => 111, -height => 333, -relief => "raised", -borderwidth => 2, -bg => '#F0F000', )->pack( -fill => 'both', -side => 'left', -anchor => 'n', ); for ( qw[Name: Version: Description: Date: ] ){ $l->Label( -text => $_, -justify => 'left', -anchor => 'w', )->pack( #-anchor => 'e', # if you don't fill -fill => 'x', ); $r->Label( -text => $_, # -justify => 'right', -anchor => 'e', )->pack( -anchor => 'e', # if you don't fill #-fill => 'x', ); $r2->Label( -text => $_, -justify => 'right', # -anchor => 'e', )->pack( #-anchor => 'e', # if you don't fill #-fill => 'x', ); } #$top->Label(-text => "Enter the scroll frame")->pack; MainLoop;

    I'm not really a human, but I play one on earth CandyGram for Mongo
Re: Perl Tk Spaces in Text of Labels
by Anonymous Monk on Jul 02, 2008 at 08:59 UTC
    Create more than one label, and use pack/grid/... them to get to line up.
Re: Perl Tk Spaces in Text of Labels
by graff (Chancellor) on Jul 03, 2008 at 02:07 UTC
    As a general rule, if you use a fixed-width font for labels and other widgets, it will be easier to get things to line up nicely. If you must use proportional fonts, you either have to accept some limitations on alignment, or else you have to do a lot more work to enforce the alignment you want.

    Update: What I mean is: Perl/Tk does not really truncate or reduce consecutive spaces in the text of a label widget, but when using a proportional font, the spaces are narrower than most characters, whereas with a fixed-width font, such as "courier", every space counts for the same width as any other character, so labels with text strings like the following will come out the same width when displayed with a courier font:

    "one two" "three four"
      As ever, many thanks for the replies. I will try and sort it using the ideas.
      I can see that if this all fails I will have to send an example.
        Update:
        I have now come up with a solution that others may like to hear.
        The situation is that I want a row of headings above a vertically scrolling pane.
        The pane had frame and the frame a grid of ‘cells’ that corresponded to the heading row.
        I am using the grid packing method.
        I did want to use proportion fonts since these look better that fixed width fonts.
        I created an gif file of the headings by:
        1. getting a screen dump of the gui without the headings;
        2. using a graphics application to view the screen shot;
        3. lay out the headings as text so that these were positioned correctly:
        4. save just the headings with the background of the GUI as a gif file;
        5. used the gif file on the label.
        Perhaps not perfect but certainly acceptable.