home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeMastering Perl/TkSearch this book

23.2. 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.

Figure 23-1

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.

Figure 23-2

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 available cursors
## Might have to change this if your cursorfont.h is elsewhere
## On Win32 systems look in C:\Perl\lib\site\Tk\X11\cursorfont.h
open (FH, "/usr/X11R6/include/X11/cursorfont.h") ||
  die "Couldn't open cursor file.\n";

while (<FH>) {
  push(@cursors, $1) if (/\#define XC_(\w+) /);


$lb->insert('end', sort @cursors);
     sub { $mw->configure(-cursor => $lb->get($lb->curselection)); });


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.