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

I am looking for a simple means to overcome two (possibly with same root cause) issues relating to Tk::Listbox entry selections being reset unexpectedly (my expectations).

1. Scenario Tk::Entry (or Tk::ROText both tested) widget with text showing, Tk::Listbox with one or more entries selected (-selectmode 'single' and 'extended' tested) and select (Left Mouse press and drag) some text in Tk::Entry and you'll see the Listbox selection is reset.

2. Have multiple Tk::Listboxs open, even if in separate toplevel windows, and you cannot (as far as I can see) have selections in more than one Listbox at a time - even if you use selectionSet() method to try and achieve that (selection in more than one Listbox at a time that is).


Doctor it hurts when I do this! Reply: Then don't do that! So yes, not without a measure of frustration, I can achieve what I want via using <<ListboxSelect>> event and depicting selected entries in a different font color and/or background plus code to do equivalent of curselection() method by fnding such configuration item differences or I could look to Tk::TableMatrix and have multiple columns(lists) etc



Education greatly appreciated!
To add to the frustration, it appears my lap counter is in need of replacement due to RSI - I've been around the bend too often! However, I can't afford a replacement due to the therapy costs between counter update events!

  • Comment on Tk::Listbox and Selection unexpected changes

Replies are listed 'Best First'.
Re: Tk::Listbox and Selection unexpected changes
by kschwab (Vicar) on Nov 24, 2018 at 03:58 UTC
    Set "-exportselection" to 0 on both of the widgets.

      Thank you - indeed simple and a fast response.
      I was just about to add that I had eliminated bindings from the equation via removing all of them on the listbox (bindtags([])) and using selectionSet() to add one before changing the text in Entry with same outcome when I saw your reply. I have tested with -exportselection 0 & 1 on the Entry Widget but 0 on the Listbox and it indeed behaves as expected(by me).

      If this is behavior is documented somewhere I clearly missed it (along with -exportselection being on by default which is documented) - I assure you I did multiple searches and lots of 'what if I try' before posting.

      Again thank you for your expertise and time to share it!

        "I assure you I did multiple searches and lots of..."
        Ah, don't sweat that. There's a reason I was able to answer quickly. Stuck on that same problem in the past :)
Re: Tk::Listbox and Selection unexpected changes
by tybalt89 (Monsignor) on Nov 24, 2018 at 04:01 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1226249 use strict; use warnings; use Tk; use Tk::ROText; my $message = ''; my $lb1pick = ''; my $lb2pick = ''; my $mw = MainWindow->new; my $label = $mw->Label( -textvariable => \$message, )->pack(); my $lb1 = $mw->Listbox( -exportselection => 0, )->pack(-side => 'left'); my $lb2 = $mw->Listbox( -exportselection => 0, )->pack(-side => 'left'); $lb1->insert(end => qw( one two three four five ) ); $lb2->insert(end => qw( six seven eight nine ten ) ); $lb1->bind('<ButtonRelease-1>' => sub { $lb1pick = $lb1->get('active'); $message = $lb1pick . ' ' . $lb2pick; }); $lb2->bind('<ButtonRelease-1>' => sub { $lb2pick = $lb2->get('active'); $message = $lb1pick . ' ' . $lb2pick; }); MainLoop;

      Thank you for your time and code. The penny dropped (which I quickly picked up to add to my therapist fund) when I saw the comment re exportselection.

      "Every man is a damn fool for at least five minutes a day. Wisdom comes in not exceeding that limit." - Elbert Hubbard
      I have exceeded my limit for the day! And in the interest of political correctness I was going to replace 'man' in the quote by entity but that would imply a woman (or alien for that matter in deference to the entity having the human experience) can be foolish and as a married man it would be foolish of me to imply that, as for aliens (assuming they have a choice) well they are just foolish for being here.