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


Hi Monks,

I create strings which are formatted with sprintf,
then these strings are printed to stdout and inserted into the ListBox.
The problem is that strings are not aligned in the ListBox.
I will appreciate any help with this, thank you in advance.
#!C:/Perl/bin/perl.exe -w use Tk; use strict; use warnings; my $mw = MainWindow->new( -title => 'TEST' ); my $entry = $mw->Listbox( -height => 12, -width => 40 )->pack(); my $var = sprintf( "%-20s", "MAX VALUE" ) . " = 100"; my $var1 = sprintf( "%-20s", "MIN " ) . " = 100"; my @output = ( $var, $var1 ); foreach (@output) { print $_, "\n"; $entry->insert( 'end', $_ ); } MainLoop;

Replies are listed 'Best First'.
Re: Aligning output on a ListBox Tk
by gmargo (Hermit) on Dec 19, 2009 at 10:30 UTC

    The Listbox is using a proportional font. Change the font to a fixed width one.

Re: Aligning output on a ListBox Tk
by zentara (Cardinal) on Dec 19, 2009 at 15:08 UTC
    ..... counting font width for alignment is usually a quick, but bad method of aligning ..... you might want to switch to a better widget.... you could do something like this, with a checkbutton next to each to select in a scrolled pane
    #!/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.
    Old Perl Programmer Haiku
Re: Aligning output on a ListBox Tk
by ramzik (Initiate) on Dec 19, 2009 at 10:47 UTC
    Thaks a lot!