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

Hi Monks,

In the below code, if the user clicks on APPLE(which will be displayed) under ONE, i need to know the path 'one.apple' This is currently possible but only with double-click(using -command). How can i change this binding to single click?

Quoting from HList in CPAN-> Switch: -command Specifies the perl/Tk callback to be executed when the user invokes a list entry in the HList widget. Normally the user invokes a list entry by double-clicking it or pressing the Return key.

use Tk; use Tk::HList; my $mw = MainWindow->new(-title=>'HList'); my $hlist = $mw->HList->pack; foreach(qw/one two three one.apple two.grapes three.apple/){ my $path = $_; (my $display_entry = $_) =~ s/.*\.//; my $uc_entry = uc($display_entry); $hlist->add($_, -text => $uc_entry); } MainLoop;
Thanks RJ

Replies are listed 'Best First'.
Re: How to bind single click to get an path of an entry in Tk::HList
by kcott (Archbishop) on Apr 17, 2014 at 22:58 UTC

    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

      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

Re: How to bind single click to get an path of an entry in Tk::HList
by zentara (Cardinal) on Apr 18, 2014 at 09:13 UTC
    From my memory of Hlist, I think you can get the closest element to a x-y mouse event. Here is a an old tip I found:

    ======== > atm when I right click on an entry in my hlist it doesnt select the > entry currently under the mouse pointer (ie explorer like behaviour) +. > Can some1 give me a few pointers. > I've had the same problem and this is my solution - when user press <Button-3> we send <Button-1> with same coords. $tree->bind("<Button-3>" => [\&menu_popup, Ev('x'), Ev('y')]); sub menu_popup { my ($wg,$x,$y) = @_; $wg->eventGenerate('<Button-1>', -x => $x , -y => $y ); if (my $sel=$wg->info("selection")) { $menu->Popup(-popover => "cursor", -popanchor => 'nw') }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh