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

Hello monks

Is there a way to set a horizontal padding to the items inserted in a ListBox? I want the item text to be some pixel away from the border of the ListBox container. I couldn't find any option to do this ($listbox->itemconfigure is limited to a couple of options). Thank you for any suggestion.

Replies are listed 'Best First'.
Re: Tk Listbox item padx
by choroba (Cardinal) on Apr 09, 2020 at 00:06 UTC
    If you don't want to prepend a space to each element in the list, use a more advanced type of a list. For example, a Tk::HList:
    #! /usr/bin/perl use warnings; use strict; use Tk; use Tk::HList; my $mw = 'MainWindow'->new(-title => 'Listbox test'); my $hl = $mw->HList(-drawbranch => 0, -pady => 0, -padx => 10 # <- This is what you need! )->pack; for ('A' .. 'H') { $hl->addchild("", -text => $_); } MainLoop();
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Thanks, choroba, for pointing me to the HList widget. The behaviour was exactly what I was loking for.

      2020-04-10 Athanasius s/Corion/choroba/.

Re: Tk Listbox item padx
by AnomalousMonk (Archbishop) on Apr 09, 2020 at 01:07 UTC

    Not quite sure just what you want, but maybe investigate  -ipadx -ipady for internal padding, whereas  -padx -pady pad the exterior of a widget.


    Give a man a fish:  <%-{-{-{-<

      G'day AnomalousMonk,

      I don't know which widget you were thinking of. You didn't specify anything different from the Tk::Listbox widget that the OP asked about. That widget does not support any of the options you suggested. Here's two of them; the other two failed with similar messages.

      $ perl -e 'use Tk; my $mw = tkinit; $mw->Listbox(-ipadx => 1)->pack; M +ainLoop' unknown option "-ipadx" at /long/path/to/Tk/Widget.pm line 205. at -e line 1.
      $ perl -e 'use Tk; my $mw = tkinit; $mw->Listbox(-padx => 1)->pack; Ma +inLoop' unknown option "-padx" at /long/path/to/Tk/Widget.pm line 205. at -e line 1.

      And purely as a test control, this worked:

      $ perl -e 'use Tk; my $mw = tkinit; $mw->Listbox()->pack; MainLoop'

      — Ken

        It was the mention of  padx in the OP title that brought  -ipadx -ipady to mind. These are pack options, but apparently  -ipadx doesn't do quite what I imagined; this

        c:\@Work\Perl\monks>perl -wMstrict -le "use Tk; ;; my $mw = tkinit; ;; my $lb = $mw->Listbox->pack(-ipadx => 10); $lb->insert('end', $_) for 'four score and', 'seven years ago', 'our +fathers'; ;; MainLoop "
        does not produce the left-hand border/indent I expected. Oh, well...


        Give a man a fish:  <%-{-{-{-<