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


In reply to Re: Tk tagBind problem by {NULE}
in thread Tk tagBind problem by arc_of_descent

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.