Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

A question on using Tk::ItemStyle in Tk::Tree

by Anonymous Monk
on Oct 30, 2014 at 13:54 UTC ( [id://1105629]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I'm not sure what wrong I'm doing. Here's a piece of code that demonstrates the problem I'm facing.

use Tk::Tree; my $mw = MainWindow->new; my $tree = $mw->ScrlTree(-indicator=>1, -browsecmd=>\&abba_browse,) ->pack(-fill => 'both', -expand => 1); my $styleref_select_parent = $tree->ItemStyle('imagetext', -stylename => 'stylename_select_parent', -selectbackground=>'cyan', -selectforeground=>'blue', -font=> [-family => 'Linotype Birka', -size => 20, -weight => 'bold', -slant => 'roman', -underline => 1, -overstrike => 1]); my $styleref_select_child = $tree->ItemStyle('imagetext', -stylename => 'stylename_select_child', -selectbackground=>'cyan', -selectforeground=>'red', -font=> [-family => 'Linotype Birka', -size => 10, -weight => 'bold', -slant => 'roman', -underline => 0, -overstrike => 0]); $tree->add('ABC', -text => 'Text is: ABC', -state=>'normal'); $tree->add('ABC.def', -text => 'Text is: def', -state=>'normal'); $tree->add('ABC.def.ghi', -text => 'Text is: ghi', -state=>'normal'); $tree->setmode(); $tree->close('ABC.def'); my $tree_element; MainLoop; sub abba_browse { $tree_element = $tree->info('selection')->[0]; if ($tree_element eq 'ABC') { $tree->entryconfigure($tree->info('selection'), -style=>$styleref_select_parent); } else { $tree->entryconfigure($tree->info('selection'), -style=>$styleref_select_child); } }

Question 1:
        When I click on the parent entry 'Text is ABC', the ItemStyle configured for this entry doesn't get applied. i.e. while the  -selectbackground doesn't work at all, the values of the  -font option applied are those configured for the child . Why is this so ?

Question 2:
        In the code if I move the declaration for  $styleref_select_parent past that of  $styleref_select_child then only those of the parent are applied to that of child as well. Is it then that only the last declaration which sets the  -font option is considered ?

Question 3:
        Just like we can configure the font for each of the display item in the tree via the -font option, is there a way to configure the size of the indicator in tandem with font for the text entries in the tree ?


BTW: I'm using,
        Strawberry Perl version: 5.16.3
        ItemStyle.pm version: 4.004
        Tree.pm version: 4.72

Many Thanks

Replies are listed 'Best First'.
Re: A question on using Tk::ItemStyle in Tk::Tree
by choroba (Cardinal) on Oct 30, 2014 at 18:17 UTC
    I'm not sure what exactly is the problem, but the background colour seems to work if I change -selectbackground to plain -background.

    Trying to configure -selectbackground without style leads to

    Tk::Error: unknown option "-selectbackground" at /usr/lib/perl5/vendor +_perl/5.14/x86_64-cygwin-threads/Tk.pm line 251.

    which seems to indicate you can't change the option for a single entry.

    Also, remove the declaration of $tree_element and declare it inside the sub:

    sub abba_browse { my $tree_element = $tree->info('selection')->[0]; # ...
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Hello Choroba,

      Thank you for trying and being able to reproduce the problem. After a couple of attempts, I've been able to narrow the problem to two issues.

      1. setting -selectbackground at the ItemStyle level doesn't work. It can only be done at the Tree level (which mean as all entries in the tree need to have same selectbackground color). That said, there are no such issues at all with -selectforeground option.

      2. when -selectbackground is set at the ItemStyle level AND has the same value (i.e. 'cyan' in the above code for both the ItemStyle objects), the -font option gets messsed up and thereby the last -font option encountered while executing the code gets used (i.e. see Question 2: above)
      IT APPEARS TO ME TO BE A BUG IN Tk. However, I'm a perl newbie and experienced monks out here will have to confirm it first so that it can be filed to get it fixed.

      PS: I've moved the $tree_element declaration inside the sub.

      Thank you

Re: A question on using Tk::ItemStyle in Tk::Tree
by Loops (Curate) on Oct 30, 2014 at 15:40 UTC

    Don't usually have access to a Windows machine, but one was here so I gave your code a try with "strawberry-perl-5.20.1.1-32bit", and the same versions of ItemStyle and Tree as you. After adding a couple lines to the top, it worked without the problems you described:

    use strict; use warnings; use Tk; use Tk::Tree; use Tk::ItemStyle;

    Might be worth upgrading if that's an option. GL

      Hey Loops,

          Thank you for giving quick try to my problem. While I'm consistently seeing the problem when executing the code I posted, upgrading to a 5.20 may not be an option. May be I need to do some workaround. Don't know what. However, I was wondering if you can answer Question No: 3.

      Question 3:
           Just like we can configure the font for each of the display item in the tree via the -font option, is there a way to configure the size of the indicator(the plus minus signs by default) in tandem with font for the text entries in the tree ?

      Thanks again
Re: A question on using Tk::ItemStyle in Tk::Tree
by Khen1950fx (Canon) on Oct 31, 2014 at 10:38 UTC
    Incorporating the advice of Loops and choroba, I added:
    $mw->after( 'idle', [ configure => $styleref_select_parent, -background => 'cyan', ]);
    That'll start the cyan background. Give it a try.
    #!/usr/bin/perl -l use strict; use warnings; use Tk; use Tk::Tree; require Tk::ItemStyle; my $mw = new MainWindow; my $tree = $mw->ScrlTree( -indicator => 1, -browsecmd => \&abba_browse, )->pack( -fill => 'both', -expand => 1 ); my $styleref_select_parent = $tree->ItemStyle( 'imagetext', -stylename => 'stylename_select_parent', -foreground => 'dark blue', -background => 'cyan', -font => [ -family => 'Linotype Birka', -size => '-10', -weight => 'bold', -slant => 'roman', -underline => 0, -overstrike => 0 ] ); my $styleref_select_child = $tree->ItemStyle( 'imagetext', -stylename => 'stylename_select_child', -foreground => 'red', -background => 'cyan', -font => [ -family => 'Linotype Birka', -size => '20', -weight => 'bold', -slant => 'roman', -underline => 1, -overstrike => 1 ], ); $tree->add( 'ABC', -text => 'Text is: ABC', -state => 'normal', ); $tree->add( 'ABC.def', -text => 'Text is: def', -state => 'normal', ); $tree->add( 'ABC.def.ghi', -text => 'Text is: ghi', -state => 'normal', ); $tree->setmode(); $mw->after( 'idle', [ configure => $styleref_select_parent, -background => 'cyan' ], ); MainLoop; sub abba_browse { my $tree_element = $tree->info('selection')->[0]; if ( $tree_element eq 'ABC' ) { $tree->entryconfigure( $tree->info('selection'), -style => $styleref_select_parent ); } else { $tree->entryconfigure( $tree->info('selection'), -style => $styleref_select_child ); } }
      Hi Ken,

      Thank you for your response to my query. As I'm new to perl, can you please explain me little bit about what the following construct you added does ?

      $mw->after( 'idle', [ configure => $styleref_select_parent, -background => 'cyan', ]);
          I referred to Mastering Perl/Tk book(Oreilly Press), but I could not find anything being talked about this method named: after.
      1. under what circumstances is this construct useful?
      2. Also, Tk::Widget doesn't have any such method at all. So how this method gets inherited by Tk::Widget class?
      3. The anonymous array passed as second parameter suprisingly has no hyphen prefixing the configure option either.
      4. I was wondering if you have any answers for me to Question: 3 in the original post. (about the size of the font applied to + - indicators as well)

        Please let me know.
        Thanks again,
        Tk::after
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found