Retrieving Elements

The get method returns a list of Listbox elements specified by the indexes firstindex tolastindex:

$lb->get(firstindex 
            [,  lastindex ]);

If only the firstindex is specified, only one element is returned. The firstindex must be less than or equal to the lastindex. To get a list of all elements in the Listbox:

@elements = $lb->get(0, 'end');

To get the last item in the Listbox:

$lastitem = $lb->get('end');

To find out which items in the Listbox are selected, use the curselectionmethod:

@list = $lb->curselection();

It returns a list containing the indexes of all currently selected items in the Listbox. If no items are selected, curselection returns an empty string. Here is an example of how the curselection method is used:

@selected = $lb->curselection;
foreach (@selected) {
  # do something with the index in $_
}

Remember that curselection returns a list of indexes, not elements.

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.