Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Tk tagBind problem

by {NULE} (Hermit)
on Jan 18, 2003 at 18:19 UTC ( [id://228013]=note: print w/replies, xml ) Need Help??


in reply to Tk tagBind problem

Hi,

The problem is with the way callbacks work in TK. They like to receive an anonymous array with the first element being the sub to be called and subsequent elements are passed to that sub. Here is your code, modified to work.

#!/usr/bin/perl -w use strict; use Tk; use Tk::ROText; my $mw; # Main Window my $fview; # Text box (Read only) # Create Main Window and textbox $mw = new MainWindow; $fview = $mw->Scrolled( 'ROText', -scrollbars => "oe", -cursor => "hand2" ); $fview->pack; # Print "Root" Folder $fview->insert('end', "\n"); $fview->insert('end', "Root" , "Root"); $fview->insert("Root.last", "\n", "Root_newline"); # Hover over $fview->tagBind("Root", "<Enter>" => [ \&hover, "Root", 1 ]); # Hover out $fview->tagBind("Root", "<Leave>" => [ \&hover, "Root", 0 ]); # Double click $fview->tagBind("Root", "<Double-Button-1>" => [ \&dclick, "Root", 0 ]); # Wait For Christmas! MainLoop; # make folder hot! sub hover { shift; my $tag = shift; my $do = shift; if ($do == 1) { $fview->tagConfigure($tag, -foreground => "red" ); } elsif ($do == 0) { $fview->tagConfigure($tag, -foreground => "black" ); } } # expand folder sub dclick { shift; my $tag = shift; my @folders; my $fullpath; @folders = ( "Linux", "Mail", "Perl" ); foreach my $folder (@folders) { $fullpath = "/$folder"; # Hover over $fview->tagBind($fullpath, "<Enter>" => [ \&hover, $fullpath, 1 ]); # Hover out $fview->tagBind($fullpath, "<Leave>" => [ \&hover, $fullpath, 0 ]); # Diplay the folder link $fview->insert( 'Root_newline.last', $folder, $fullpath ); $fview->insert( $fullpath.".last", "\n", $fullpath."_newline" ); } }
I got ride of the sub prototypes ('cause I don't like them. :) ) and change the callbacks to the way that TK expects them. Note that I have to shift the first element off because it passes the ref of the widget performing the callback.

I conceptually understand why this behaves this way, but words fail me when I try to explain it. Hopefully one of our fellow monks will step up to the plate here. To experiment with callbacks I suggest you do a callback to a sub in this manner (Tk::Canvas style shown here) and have that sub just do this:

$w->bind("tag", '<1>', [ \&bind, "one", "two" ]); sub stuff { my @a = @_; my $i = 0; print "Callback received this stuff:\n"; for (@a) { print "\targ($i) : $_\n"; $i++ } }
Hope this helps - sorry if it's not terribly clear,

{NULE}
--
http://www.nule.org

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found