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

Hi Monks

I am trying to attach a Balloon to a text (word) in a Text widget. I am not quite sure this is possible, as it seems to me that it is possible to attach it only to a widget. I am able to highlight the word (in this example in red), but how can I attach a Balloon to it? Any idea?

use strict; use warnings; use Tk; use Tk::Text; use Tk::Balloon; my $mw = tkinit(); my $text = $mw->Text()->pack(); $text->insert('end',"This is a red text\n"); $text->tagAdd('red_text',"1.10","1.13"); $text->insert('end',"This is a black text\n"); $text->tagConfigure('red_text', -foreground => 'red'); my $balloon = $text->Balloon(); $balloon ->attach($text , -balloonmsg => "hello", -initwait=>10); $mw->MainLoop();

Replies are listed 'Best First'.
Re: Attaching a balloon message to text on the Text widget
by choroba (Cardinal) on Jan 17, 2018 at 10:56 UTC
    In the PM::CB::G chatterbox client, I use balloons to display link details, so just copying from there:
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; use Tk::Balloon; my $mw = tkinit(); my $text = $mw->Text->pack; $text->insert('end', "This is a red text\n"); $text->tagAdd('red_text', '1.10', '1.13'); $text->insert('end', "This is a black text\n"); $text->tagConfigure('red_text', -foreground => 'red'); my $balloon = $text->Balloon(); $text->tagBind('red_text', '<Enter>', sub { $balloon->attach($text , -balloonmsg => 'hello +', -initwait => 10, -balloonposition => 'mouse +'); }); $text->tagBind('red_text', '<Leave>', sub { $balloon->detach($text) }) +; $mw->MainLoop();
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      If I want to dynamically assign the value of -balloonmsg, for example the content of the tagged text? In the following I have 2 words tagged, the balloon should diasplay as message the respectiv text which is tagged. I simulate this reading the content with "get", but I use fix indexes and of course it is not okay. Any idea how to get those indexes dynamically?

      #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; use Tk::Balloon; my $mw = tkinit(); my $text = $mw->Text->pack; $text->insert('end', "This is a red text\n"); $text->tagAdd('red_text', '1.10', '1.13'); $text->insert('end', "This is another xxx text\n"); $text->tagAdd('red_text', '2.16', '2.19'); $text->tagConfigure('red_text', -foreground => 'red'); my %messages = (1=>"hello", 2=>"good"); my $balloon = $text->Balloon(); $text->tagBind('red_text', '<Enter>', sub { my $message= $text->get('1.10', '1.13');# this should become dynam +ic $balloon->attach($text , -balloonmsg => $message, -initwait => 10, -balloonposition => 'mouse'); }); $text->tagBind('red_text', '<Leave>', sub { $balloon->detach($text) }) +; $mw->MainLoop();
        If you want each tag to display a different balloon message, you need different tags:
        #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Balloon; my $tag_counter = 0; sub tag_and_balloonify { my ($text, $tag, $balloon, $x, $y) = @_; $tag .= ++$tag_counter; my %messages = (1 => 'hello', 2 => 'good'); my $message = $messages{$tag_counter}; $text->tagAdd($tag, $x, $y); $text->tagBind($tag, '<Enter>', sub { $balloon->attach($text, -balloonmsg => $message, -initwait => 10, -balloonposition => 'mouse'); }); $text->tagBind($tag, '<Leave>', sub { $balloon->detach($text) }); $text->tagConfigure($tag, -foreground => 'red'); } my $mw = tkinit(); my $text = $mw->Text->pack; my $balloon = $text->Balloon(); $text->insert('end', "This is a red text\n"); tag_and_balloonify($text, 'red_text', $balloon, '1.10', '1.13'); $text->insert('end', "This is another xxx text\n"); tag_and_balloonify($text, 'red_text', $balloon, '2.16', '2.19'); $mw->MainLoop();
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,