in reply to PerlTk - How to retrieve the item style of a tree node?

In short, you can get the style you want with:

my $wanted_style = $tree->entrycget($path_to_copy_style_from, q{-style +});

You haven't shown sufficient code for me suggest where you might have gone wrong; however, I've included a call to entryconfigure() (in the example code below) in case that's what was causing the problem.

Here's a working script that demonstrates a way to achieve what you want.

use strict; use warnings; use Tk; use Tk::Tree; use Tk::ItemStyle; my $mw = MainWindow->new(); my $style_t1 = $mw->ItemStyle(q{text}, -fg => q{red}, -bg => q{gold}); my $style_t1_2 = $mw->ItemStyle(q{text}, -fg => q{red}, -bg => q{white +}); my $indent0_line = 2; my $frame_t1 = $mw->Frame()->pack; my $frame_t2 = $mw->Frame()->pack; my $frame_b = $mw->Frame()->pack; my $tree_1 = $frame_t1->Tree(-itemtype => q{text})->pack; for (0 .. 3) { $tree_1->add($_, -text => qq{Line: $_}, -style => $style_t1); } $tree_1->entryconfigure($indent0_line, -style => $style_t1_2); my $tree_2 = $frame_t2->Tree(-itemtype => q{text})->pack; my $indent0_line_style = $tree_1->entrycget($indent0_line, q{-style}); for (0 .. 3) { $tree_2->add($_, -text => qq{Line: $_}, -style => $indent0_line_st +yle); } $frame_b->Button(-text => q{Exit}, -command => sub { exit })->pack; MainLoop;

-- Ken

Replies are listed 'Best First'.
Re^2: PerlTk - How to retrieve the item style of a tree node?
by Sicario (Initiate) on Feb 13, 2012 at 12:34 UTC
    Ken,

    Many thanks for posting a reply with complete working code. Since posting I managed to find a work-around by adding a data string to the node entry in the tree, and then pulling the info later:

    $string='Match'; # #define Itemstyles # $tree1_match_style = $tree1->ItemStyle('text', -foreground=>'green'); $tree2_match_style = $tree1->ItemStyle('text', -foreground=>'green'); # #add node to tree1 and tree2 # $tree1->add($indent0_line,-text=> $indent0_line,-itemtype=>'text'); $tree2->add($indent0_line,-text=> $indent0_line,-itemtype=>'text'); # #add style to node entry # $tree1->entryconfigure($indent0_line,-style=>$tree1_match_style); # #add data to node entry # $tree1->entryconfigure($indent0_line, -data =>$string); # #retrieve data from node # my $style_data=$tree1->infoData($indent0_line); # #retrieve style from node # my $wanted_style = $tree1->entrycget($indent0_line, q{-style}); # #print retrieved data and style # print "Tree1 style[$style_data]\n"; print "Tree1 wanted style[$wanted_style]\n"; # #configure style of node in tree2 # if ($style_data eq $string) { $tree2->entryconfigure($indent0_line,-style=>$tree2_match_style); }

    When run the output is as follows:

    Tree1 style[Match] Tree1 wanted style[Tk::ItemStyle=HASH(0x21dd6d4)]

    However I've just used the pertinent lines from your post/code to shorten all the above to the follows, and it works a treat:

    $string='Match'; # #define Itemstyles # $tree1_match_style = $tree1->ItemStyle('text', -foreground=>'green'); $tree2_match_style = $tree1->ItemStyle('text', -foreground=>'green'); # #add node to tree1 and tree2 # $tree1->add($indent0_line,-text=> $indent0_line,-itemtype=>'text'); $tree2->add($indent0_line,-text=> $indent0_line,-itemtype=>'text'); # #add style to node entry # $tree1->entryconfigure($indent0_line,-style=>$tree1_match_style); # # #retrieve style from node # my $wanted_style = $tree1->entrycget($indent0_line, q{-style}); # #configure tree2 node with retrieved style from tree1 node # $tree2->entryconfigure($indent0_line,-style=>$wanted_style);

    The pertinent line in all this is the entrycget, and despite all my previous searching I was not aware you could retrieve the style of an item (hence my posting!)

    Again, many thanks for taking the time and effort to reply and post a working script.