http://qs321.pair.com?node_id=11110970

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

Hello Monks

I need to monitor if a new element has been programmatically added to a Listbox. If a new element has been added, a subroutine should be called. Since I am not aware of any callback for this, in the script below, I try to use programmatically select a Listbox element with selectionSet (whenever a new element is added) in order to let the <<ListboxSelect>> call a subroutine. The approach does not work, since <<ListboxSelect>> seems to be called only if a Listbox element is selected by the user. Using <<Modified>> does not work too.

Any suggestion on how I could solve this?

use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $lbox = $mw->Listbox()->pack(); my @list = ( "a", "b", "c", "d", "e", "f" ); $lbox->insert('end', @list ); $lbox->selectionSet('1'); #$mw->Button(-text => "Set!", -command=>sub{$lbox->selectionSet('2');} +)->pack();#to test if sectionSet works with <<ListboxSelect>> $lbox->bind('<<ListboxSelect>>', sub{warn "ListBox changed\n"} ); $mw->MainLoop;

EDIT

The code above is in Perl/Tk and not in Tcl::Tk simply because I was looking for a solution independently of Tcl::Tk, as I thought a callback triggered by a state change in the Listbox was easier/possible to achieve then working on the Tcl bridge to Perl through Tcl::Tk (or the similar Tcl::pTk)