Book HomeMastering Perl/TkSearch this book

23.5. The Balloon Widget

Using a Balloon widget, you can create help-text-like labels that appear as the mouse hovers over a widget. You can also use it to create help text that appears in a status bar. Let's look at a very simple example, then go over the relevant options and methods:

use Tk;
use Tk::Balloon;

$mw = MainWindow->new(-title => "Simple Balloon example");
$button = $mw->Button(-text => "Exit", -command => sub { exit })->pack;
$msgarea = $mw->Label(-borderwidth => 2, -relief => 'groove')
  ->pack(-side => 'bottom', fill => 'x');
$balloon = $mw->Balloon(-statusbar => $msgarea);
$balloon->attach($button, -balloonmsg => "Exit the App",  
  -statusmsg => "Press the Button to exit the application");
$balloon->attach($msgarea, -msg => 'Displays the help text for a widget');
MainLoop;

Figure 23-12 illustrates this example.

Figure 23-12

Figure 23-12. Using a Balloon as both help text and a status message

Using a status bar is optional. We've just included it here to show you how easy it is. Here are the options you can use when creating your Balloon widget:[68]

[68] Not all the options are detailed here, only those used 99% of the time. See the documentation included with the widget for a complete listing.

-state => 'balloon' | 'status' | 'both' | 'none'
Determines if the Balloon widget will be displaying only balloons, only status messages, both (the default), or nothing.

-statusbar => widget
Tells the Balloon what widget to use for displaying status messages. The widget specified must have a -text or -textvariable option.

-balloonposition => 'widget' | 'mouse'
Determines the position in which the Balloon is displayed. Specify mouse to use the cursor position.

-initwait => time
The amount of time in milliseconds to wait before showing the balloon message or status message. Default is 350 milliseconds.

Once the Balloon is created, you can both attach and detach widgets. Here's the attach method:

$balloon->attach($widget, option => value, ... );

This method takes a widget and a list of the following option/value pairs:

-msg => string
The message to be displayed in both a balloon and the status bar

-balloonmsg => string
The message to be displayed only as a balloon

-statusmsg => string
The message to be displayed only in the status bar

You can override any of the -initwait, -state, -statusbar, or -balloonposition options with each individual call to attach.

The balloon demo included with the widget demo application is quite good; it shows attaching a Balloon to text widgets, canvas items, and various other widgets.



Library Navigation Links

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