16.14. Selection Methods

Some widgets allow the user to make a selection. For example, the user can make a selection in the text, entry, and listbox widgets. You can manipulate the selection by using the following methods.

16.14.1. Clearing the Selection

To clear the current selection from any widget (this will also clear an X selection) use SelectionClear:

$widget->SelectionClear();

You can specify a -selection option, which takes either "PRIMARY" or "CLIPBOARD". The default is "PRIMARY". Using "CLIPBOARD" clears out the clipboard as well.

16.14.2. Getting the Selection

To determine what the current selection for the application is, use SelectionGet:

$selection = $widget->SelectionGet();

You can also specify the -selection option with the SelectionGetmethod:

$clipboard = $widget->SelectionGet(-selection => "CLIPBOARD");

The -selection method takes either "PRIMARY" or "CLIPBOARD". The default is "PRIMARY", so if you don't specify -selection, you will get back the value that represents the current selection in the application. Using "CLIPBOARD" will return the value in the clipboard.

16.14.3. Assigning a Callback

You can call SelectionHandle to assign a callback that will automatically be invoked when the selection associated with $win changes:

$widget->SelectionHandle($win => \&subroutine);

When $win owns the selection, the callback will be invoked (in this example, subroutine). You can specify the options -format, -type, and -selection with the same possible values shown ...

Get Learning 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.