Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
Looking for a little help in ataaching a balloon to text on the canvas, not the whole canvas.
The code below is broken unless you change the $text after attach to $c but that makes the balloon and msg pop up anytime you enter the canvas which is not what I want.
I know it's probably a simple fix but I'm a newbie.
Thanks,
-Mark
use Tk; use Tk::Balloon; use Tk::Canvas; $mw = MainWindow->new; $c=$mw ->Scrolled("Canvas", -cursor => "crosshair")->pack( -side=> "left", -fill => 'both', -expand =>1); $text = $c->createText(50,50, -fill=>'blue', -text=>"test"); #$button = $mw->createText(-text => "Exit", -command =>sub {exit})->pa +ck; $msgarea =$c->Label(-borderwidth => 2, -relief => 'groove') ->pack(-side => 'bottom', fill => 'x'); $balloon = $c->Balloon(-statusbar =>$msgarea); $balloon->attach($text, -balloonmsg => "hi", -statusmsg => "hi"); MainLoop;

Replies are listed 'Best First'.
Re: Attaching a balloon message to text on the canvas
by zentara (Cardinal) on Sep 25, 2003 at 18:51 UTC
    Does this work for you?
    #!/usr/bin/perl use Tk; use Tk::Balloon; my $mw = MainWindow->new; my $canvas = $mw->Canvas()->pack( -expand => 1, -fill => 'both', ); my $id = $canvas->createText( 5, 5, -text => 'hello', -anchor => 'nw', ); my %messages = (); $messages{$id} = "there"; my $balloon = $canvas->Balloon(); $balloon->attach( $canvas, -balloonposition => 'mouse', -msg => \%messages, ); MainLoop();
      You beat me to it, zentara. The method of attaching the balloon to the canvas, but then calling the -msg => \%messages structure does solve the problem. However, it seems not to work on a "Scrolled" canvas. For some reason, when using the Scrolled("Canvas", etc...) syntax, the message returns a "hash ref...." type message, and returns it on the whole canvas, not merely the portion you want.

      So the likely solution is for you to do a regular canvas, add scrollbars the old manual way, and use the above hash format (which is available for easy browsing in the widget demo that perl tk comes with). I hope this helps you solve your problem. Do look at the widget demo for code ideas on this, as they have a good example of exactly this type of code (minus the scrolled version).
      Thanks all! -Mark
Re: Attaching a balloon message to text on the canvas
by zentara (Cardinal) on Sep 28, 2003 at 11:00 UTC
    Hi, I found the answer on comp.lang.per.tk. Martin Raspe wrote:

    If a canvas is scrolled, it is wrapped in a frame. Attach the widget to the canvas itself: $balloon->attach($canvas->Subwidget('canvas'), ...

    So to make my example work with scrolled canvas......

    #!/usr/bin/perl use Tk; use Tk::Balloon; # the scrolled canvas causes problems with balloon # unless you use the subwidget my $mw = MainWindow->new; my $canvas = $mw->Scrolled('Canvas', -takefocus => 0)->pack( -expand => 1, -fill => 'both', ); my $id = $canvas->createText( 5, 5, -text => 'hello', -anchor => 'nw', ); my %messages = (); $messages{$id} = "there"; my $balloon = $canvas->Balloon(); $balloon->attach($canvas->Subwidget('canvas'), -balloonposition => 'mouse', -msg => \%messages, ); MainLoop();