Manipulating the Cursor

Every Tk widget has a cursor shape associated with it. Most default to what’s known as the left_ptr shape, shown in Figure 23-1.

The standard cursor for most widgets

Figure 23-1. The standard cursor for most widgets

The cursor shape can be changed on a widget-by-widget basis with the -cursor option:

$mw->Button(-text => 'Go ...', -cursor => cursor_name);

When the mouse is over the Button, the cursor changes to the one specified. The cursor change happens whether the Button is disabled or not. The set of available cursors is quite large. Figure 23-2 shows the cursor shapes available on most systems.

Cursor shapes available on most systems

Figure 23-2.  Cursor shapes available on most systems

Here’s a program to look at the different cursors interactively. It’s really simple, just a Listbox full of cursor shape names and a button binding that changes the application’s cursor. The hardest part is figuring out where the list of cursor names is hidden.

$mw = MainWindow->new; $mw->Button(-text => "Exit", -command => sub { exit })->pack(-side => "bottom", -fill => "x"); $scroll = $mw->Scrollbar; $lb = $mw->Listbox(-selectmode => 'single', -yscrollcommand => [set => $scroll]); $scroll->configure(-command => [yview => $lb]); $scroll->pack(-side => 'right', -fill => 'y'); $lb->pack(-side => 'left', -fill => 'both'); ## Open file that contains all ...

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.