in reply to Tk (Tcl::Tk) programmatically monitor change in Listbox

If you don't feel like patching Tk (probably somewhere here), you can check for the selection periodically using a timer:
#! /usr/bin/perl use strict; use warnings; use Tk; my $mw = 'MainWindow'->new; my $lbox = $mw->Listbox->pack; my @list = qw( a b c d e f ); $lbox->insert(end => @list); $lbox->selectionSet(1); my @selection; $lbox->repeat(100, sub { my @current = $lbox->curselection; warn "Listbox changed!\n" if "@selection" ne "@current"; @selection = @current; }); my $f = $mw->Frame->pack; $f->Button(-text => 'Add', -command => sub { $lbox->selectionSet(2) })->pack(-side => +'left'); $f->Button(-text => 'Clear', -command => sub { $lbox->selectionClear(0, 'end') })->pack; MainLoop();
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Tk programmatically monitor change in Listbox
by IB2017 (Pilgrim) on Jan 06, 2020 at 09:44 UTC

    Thank you for the idea with repeat. However, I am not sure if this will have negative implications for my app, if this is set to "run" all the time when the UI is open. I will need to test it. Probably, hacking the source will be a better solution.