Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Attaching a balloon message to text on the Text widget

by Anonymous Monk
on Jan 17, 2018 at 15:23 UTC ( [id://1207420]=note: print w/replies, xml ) Need Help??


in reply to Re: Attaching a balloon message to text on the Text widget
in thread Attaching a balloon message to text on the Text widget

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();

Replies are listed 'Best First'.
Re^3: Attaching a balloon message to text on the Text widget
by choroba (Cardinal) on Jan 17, 2018 at 15:44 UTC
    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,

      Thanks, brilliant. I have all the ingredients to work on and adapt it to my needs.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1207420]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found