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

Hi, I am using Tk::Tree along with Tk::ItemStyle. Is there any way to find out the style of each/any node of the tree?

I have 2 trees '$tree_1' and '$tree_2'. I am wanting to copy the style of node '$indent0_line' from '$tree_1' to the entry in '$tree_2' (not shown in code below), and to do so I want to get attributes of the node in '$tree_1'.

I've done an extensive search online but can't find any info about where the style of an item is stored or how to retrieve it. Anyone got any ideas please? Thanks,

use Tk::Tree; use Tk::ItemStyle; $missing_style = $tree_1->ItemStyle('text', -foreground=>'red', -activebackground=>'red'); $tree_1->entryconfigure($indent0_line,-style=>$missing_style);

Replies are listed 'Best First'.
Re: PerlTk - How to retrieve the item style of a tree node?
by kcott (Archbishop) on Feb 13, 2012 at 00:51 UTC

    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

      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.