in reply to How to bind single click to get an path of an entry in Tk::HList

G'day rjohn1,

I think the easiest option is to use -browsecmd instead of -command. Here's an example:

#!/usr/bin/env perl use strict; use warnings; use Tk; use Tk::HList; my $mw = MainWindow->new(); my $action_F = $mw->Frame()->pack(-side => 'bottom'); $action_F->Button(-text => 'Exit', -command => sub { exit })->pack; my $app_F = $mw->Frame()->pack(-side => 'top'); my $label_text = ''; my $label = $app_F->Label(-textvariable => \$label_text); $label->pack(-side => 'top', -fill => 'x'); my $hlist = $app_F->HList( -itemtype => 'text', -selectmode => 'single', -browsecmd => sub { $label_text = shift }, ); my @items = qw{one two three one.apple two.grapes three.apple}; $hlist->add($_, -text => uc((split /[.]/)[-1])) for @items; $hlist->pack(-side => 'top', -fill => 'both'); MainLoop;

The first argument to the -browsecmd callback will be the path: you can capture that and process it however you want. In this example, it's just displayed in the Tk::Label.

-- Ken

Replies are listed 'Best First'.
Re^2:How to bind single click to get an path of an entry in Tk::HList
by rjohn1 (Sexton) on Apr 18, 2014 at 04:06 UTC

    Thanks a lot for the example Ken

    Looks like i misunderstood -browsecommand option, this works great for the -selectmode=>single option, but i could not get it working for extended option. It would have been great to get an array of paths. Any ideas how?

      This change to the code creating the Tk::HList widget will cause a changing, pipe ('|') separated, list of paths to appear in the Tk::Label as you move the mouse and highlight different parts of the tree.

      my $hlist; $hlist = $app_F->HList( -itemtype => 'text', -selectmode => 'extended', -browsecmd => sub { $label_text = join '|' => $hlist->selectionGet + }, );

      The (possibly) odd-looking "my $hlist; $hlist = ..." is intended: $hlist is used in the -browsecmd callback, so it needs to be declared before the widget is created and assigned to that variable. There's no syntactical reason for putting the separate declaration and assignment on the same line: use whatever style suits you.

      As before, process the array returned by $hlist->selectionGet however you want. Also, see the Tk::HList documentation for how this behaves differently in scalar context.

      -- Ken

        Great Ken! Exactly what i wanted. Very happy :)

        Though i should be content with this, i am curious to understand how did you get this solution? selectionGet gives you the entries selected and not the paths(as per the spec). Then how did you guess that calling it within browsecommand will get the paths

        Once again Thanks!!