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

It just needs to be in single mode to grab a string. I can't seem to get it to behave at all, everything I have tried fails.

Replies are listed 'Best First'.
Re: Grabbing a variable a a tk listbox.
by Caillte (Friar) on Apr 16, 2002 at 11:32 UTC

    This simple script may help

    #!/usr/bin/perl -w use Tk ; use strict; my $top = MainWindow->new; my @words = qw(foo fum far bim baz); my $list = $top->ScrlListbox(-scrollbars => 'e', -width => 70) ; $list->pack ; my $label = $top->Label(-text => ''); $label->pack(-fill => 'both', -expand => 1); $list->bind('<1>' => [sub {&showword($list, $label)} ] ) ; grep ($list->insert('end',$_), @words) ; MainLoop ; sub showword{ my $w = shift ; my $lab = shift; my $word ; $word = $w->get($w->curselection) ; $lab->configure(-text => $word ) ; }

    This page is intentionally left justified.

Re: Grabbing a variable a a tk listbox.
by zentara (Cardinal) on Apr 16, 2002 at 16:42 UTC
    Here's another example for you. Double click a list entry to grab it.
    #!/usr/bin/perl use warnings; use diagnostics; use Tk; $top = MainWindow->new(); $wine_list = $top->Listbox("width" => 20, "height" => 5 )->pack(); $wine_list->insert('end', # Insert the following list at end "Napa Valley Chardonnay", "Cabernet Sauvignon", "Dry Chenin Blanc", "Merlot", "Sangiovese" ); $wine_list->bind('<Double-1>', \&buy_wine); sub buy_wine { my $wine = $wine_list->get('active'); return if (!$wine); # Return if no list item is active print "Ah, '$wine'. An excellent choice\n"; # Remove the wine from the inventory #$wine_list->delete('active'); } MainLoop();