in reply to Perl/TK listbox question

You just need to use -exportselection=>0; A simple example
#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my $f = $mw->Frame(-border => 1) ->pack(-side => "left", -fill => "both"); my $envs_list; $envs_list = $f->Scrolled("Listbox", -scrollbars=>"e", -exportselection => 0, -selectmode=>"extended")->pack(-fill => 'both'); $envs_list->insert("end", "abcda"); $envs_list->insert("end", "abcdb"); $envs_list->insert("end", "abcdc"); $envs_list->insert("end", "abcdd"); my $list; $list = $f->Scrolled("Listbox", -scrollbars=>"e", -exportselection => 0, -selectmode=>"extended")->pack(-fill => 'both'); $list->insert("end", "abcd1"); $list->insert("end", "abcd2"); $list->insert("end", "abcd3"); $list->insert("end", "abcd4"); $list->insert("end", "abcd5"); $mw->Button( -text => "Selections", -command => sub{ my $selected_env_list = $envs_list->get( $envs_list->curselection ); my $selected_list = $list->get( $list->curselection ); print "selected-> $selected_env_list $selected_list\n"; } )->pack( -side => 'left' ); $mw->Button( -text => "Exit", -command => \&exit )->pack; MainLoop;

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: Perl/TK listbox question
by jdporter (Paladin) on Apr 23, 2008 at 18:05 UTC
    use -exportselection=>0

    That is so great. I wish I could ++ you more. But why is your "simple example" so complicated?

    use Tk; my $mw = new MainWindow; $mw->Listbox( -listvariable => $_, -exportselection => 0 )->pack for [11..15], [21..25]; MainLoop;
    A word spoken in Mind will reach its own level, in the objective world, by its own weight
      Would you believe that I wanted to give the OP some experience sorting thru lousy long variable names? :-)

      To be honest, it was early, and I just pasted the first example I came across, but now that I look closer, it does have alot of fluff in it. :-)

      But in my defense, I had a simple non-complicated example like yours, BUT, the OP claimed unfamiliarity with pTk, so I wanted an example that was very elementary, showing each step in excruciating newbie detail....... the adding to the listboxes, and how to get both selections with a button(or command). Also, your for() syntax is clever, but not standard, and I would hate to have a new user thinking that was a good template for setting up listboxes.

      Here is how I would do a rewrite

      #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my $f = $mw->Frame( -border => 1 )->pack( -side => "left", -fill => "b +oth" ); #just manipulate arrays to change lists my @list1 = ( 'a' .. 'z' ); my @list2 = ( 1 .. 100 ); my $list1 = $f->Scrolled( "Listbox", -listvariable => \@list1, -scrollbars => "osoe", -exportselection => 0, -selectmode => "extended" )->pack( -fill => 'both' ); my $list2 = $f->Scrolled( "Listbox", -listvariable => \@list2, -scrollbars => "osoe", -exportselection => 0, -selectmode => "extended" )->pack( -fill => 'both' ); $mw->Button( -text => "Selections", -command => sub { my $selected1 = $list1->get( $list1->curselection ); my $selected2 = $list2->get( $list2->curselection ); print "selected-> $selected1 $selected2\n"; } )->pack( -side => 'left' ); $mw->Button( -text => "Exit", -command => sub { exit } )->pack; MainLoop;

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum

        Thanks for your replies. Unfortunately, for reasons beyond my control, Win32::GUI is not available for me to use. What I did was take the advice of both anonymous monk and Khen1950fx, and modify the Perl module MListbox.pm:

        First I added Control-c capabilities. Note that I need to account for lower case 'c' and upper case 'C', for when capslock is turned on:

        sub ClassInit { my ($class,$mw) = @_; $mw->bind($class,'<Configure>',['_yscrollCallback']); $mw->bind($class,'<Down>',['_upDown',1]); $mw->bind($class,'<Up>', ['_upDown',-1]); $mw->bind($class,'<Shift-Up>', ['_extendUpDown',-1]); $mw->bind($class,'<Shift-Down>',['_extendUpDown',1]); $mw->bind($class,'<Control-Home>','_cntrlHome'); $mw->bind($class,'<Control-End>','_cntrlEnd'); $mw->bind($class,'<Shift-Control-Home>',['_dataExtend',0]); $mw->bind($class,'<Shift-Control-End>',['_dataExtend',Ev('index', +'end')]); $mw->bind($class,'<Control-slash>','_selectAll'); $mw->bind($class,'<Control-backslash>','_deselectAll'); $mw->bind($class,'<Control-c>','_copyAll'); $mw->bind($class,'<Control-C>','_copyAll'); }

        Then I added the following subroutine to the MListbox.pm module. Assuming I was using Khen1950fx's example, if I wanted to be copying column #6 to the clipboard:

        sub _copyAll { my ($w) = @_; my @sel = $w->curselection; if (scalar(@sel) > 0){ $w->clipboardClear; foreach(@sel){ my ($name) = ($w->getRow($_))[6]; $w->clipboardAppend($name); } } }

        A bit clunkish, but gets the job done. Any faster ways you can think of to get the stuff copied to the clipboard without going through a loop?