http://www.perlmonks.org?node_id=1201085

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

Refer to https://i.stack.imgur.com/qVg5P.png picture.

I adding the content into a ListboxSelect of Tk GUI by following format code, seems in the output of command line, everything works and align correctly. but when the content shown in the GUI, always not align as I wanted.

Any suggestion on this issue?

$lb_ccrdetail->insert( 'end', sprintf( "%s%s%-50s%s", $ccidstring, ' 'x (15-length($ccidstring)), $prodstring, #' 'x (20-length($prodstring)), $detailstring ) );

Replies are listed 'Best First'.
Re: How to align the contents inside a ListboxSelect of Tk GUI -- updated
by Discipulus (Canon) on Oct 10, 2017 at 16:32 UTC
    If a monospaced font is the solution (as it let you to align chars), it's very easy to use it in Tk. Tk::Font names Courier as a monospaced ``typewriter'' font.

    You can create a font in your Tk application, for a later use:

    $mono_font = $mw->fontCreate('mono', -family => 'Courier', -size => 10 +);

    After you can use it by name mono or by using the variable $mono_font

    $mw->Label(-text => "test monospaced font", -font => 'mono'); $mw->Label(-text => "test monospaced font bis", -font => $mono_font);

    If you want all object of some class, or all widgets in the program to have the same font (or color, or size..) you can use the "options database" Tk uses:

    $mw->optionAdd('*font', 'Courier 12');

    L*

    PS

    You can specify -font directly int Text or ROText object, you'll anyway need a padding tecnique:

    #pay attention to win32 doublequotes! perl -MTk -e "$mw=tkinit;$t=$mw->Text(-font=>'Courier')->pack;$t->inse +rt('end', $_.(' ' x (10 - length $_)).qq(xxx\n)) for qw (aaaa bbb ccc +ccc);MainLoop"

    PPS see Tk font size for ALL widgets

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thanks for your great support! now I fix the issue by your method. everything works!
Re: How to align the contents inside a ListboxSelect of Tk GUI
by roboticus (Chancellor) on Oct 10, 2017 at 12:46 UTC

    Evel:

    I think the anonymous poster above meant change to a monospaced font, which will make the characters all take the same width:

    iiiiiiiii

    wwwwwwwww

    Where a typical variable pitch font lets normally-narrow character take less horizontal space than a normally-wide character:

    iiiiiiiii

    wwwwwwwww

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      as an example, with Tkx i do :

      Tkx::font_configure('TkFixedFont',-size=>8); .... my $lb = $w_bfrm->new_tk__listbox( -height =>3 , -selectmode=>'extended' , -font =>'TkFixedFont' );

        I using Tk, anyway thanks for your input.
      Thanks, I just fix it by replacing the original default font with mono font. and curiosity I also want to know what the default font style name is?

        G'day Evel,

        "... I also want to know what the default font style name is?"

        You can use the cget() method (see Tk::options) to determine the default option value of any widget.

        You can find values quickly using a one-liner, e.g.

        $ perl -E 'use Tk; say MainWindow->new->Listbox->cget("-background")' #d9d9d9

        Some option values aren't individual scalars: you may get a reference returned, e.g.

        $ perl -E 'use Tk; say MainWindow->new->Listbox->cget("-font")' Tk::Font=SCALAR(0x7f972e1bbd80)

        You can just dereference as normal. Here's two examples showing the older "${ ... }", and newer "...->$*", syntaxes. See "perlref: Postfix Dereference Syntax" if you're unfamiliar with the second one.

        $ perl -E 'use Tk; say ${ MainWindow->new->Listbox->cget("-font") }' Helvetica -12 bold $ perl -E 'use Tk; say MainWindow->new->Listbox->cget("-font")->$*' Helvetica -12 bold

        Here's another example just to show different widgets have different defaults.

        $ perl -E 'use Tk; say MainWindow->new->Text->cget("-font")->$*' Courier -12

        Also be aware that, while Tk has its own defaults, these may be altered externally (e.g. a .Xdefaults file); or from within your application by a module you've loaded (e.g. a custom widget), or by your own code (e.g. one of the "option*()" methods). See Tk::option for details.

        — Ken

Re: How to align the contents inside a ListboxSelect of Tk GUI
by soonix (Canon) on Oct 12, 2017 at 02:32 UTC
Re: How to align the contents inside a ListboxSelect of Tk GUI
by Anonymous Monk on Oct 10, 2017 at 10:50 UTC
    Change the font