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

I'd like to set the background color of a Tk::Tree node to some shade of red and am having trouble doing it. My current code looks like this:

use warnings; use strict; use Tk; use Tk::Tree; my $main = MainWindow->new (-title => "Test tree"); my $tree = $main->ScrlTree ( -itemtype => 'text', -separator => '/', -scrollbars => "osoe" ); $tree->add ( 1, -text => 'Some sample text', -itemtype => 'text', -bg => 'red' ); $tree->autosetmode; $tree->close (1); $tree->pack(-fill=>'both',-expand => 1); MainLoop;

When run I get the error message unknown option "-bg" at C:/Perl/site/lib/Tk.pm line 247.

Where am I going wrong?


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re: Set background on Tk::Tree nodes
by pg (Canon) on Jul 18, 2005 at 03:12 UTC

    -bg is not an option for add(), but you are close, just need to define ItemStyle.

    use Tk; use Tk::Tree; use Tk::ItemStyle; use warnings; use strict; my $main = MainWindow->new (-title => "Test tree"); my $tree = $main->ScrlTree(-itemtype => 'text', -separator => '/', -s +crollbars => "osoe"); my $red_style = $tree->ItemStyle('text', -refwindow => $tree, -bg => ' +red'); my $green_style = $tree->ItemStyle('text', -refwindow => $tree, -bg => + 'green'); $tree->add(1, -text => 'abcd', -itemtype => 'text', -style => $red_sty +le); $tree->add(2, -text => '1234', -itemtype => 'text', -style => $green_s +tyle); $tree->autosetmode; $tree->close (1); $tree->pack(-fill=>'both',-expand => 1); MainLoop;

      Excellent, just what I needed to know.

      Follow up question: How do I use an RGB value to set the color?


      Perl is Huffman encoded by design.

        Just specify RGB in the format of '#rrggbb', for example, yellow would be '#FFFF00'.

        use Tk; use Tk::Tree; use Tk::ItemStyle; use warnings; use strict; my $main = MainWindow->new (-title => "Test tree"); my $tree = $main->ScrlTree(-itemtype => 'text', -separator => '/', -s +crollbars => "osoe"); my $yellow_style = $tree->ItemStyle('text', -refwindow => $tree, -bg = +> '#FFFF00'); my $green_style = $tree->ItemStyle('text', -refwindow => $tree, -bg => + 'green'); $tree->add(1, -text => 'abcd', -itemtype => 'text', -style => $yellow_ +style); $tree->add(2, -text => '1234', -itemtype => 'text', -style => $green_s +tyle); $tree->autosetmode; $tree->close (1); $tree->pack(-fill=>'both',-expand => 1); MainLoop;
Re: Set background on Tk::Tree nodes
by BrowserUk (Patriarch) on Jul 18, 2005 at 03:25 UTC

    You should pass the bg option when you instantiate the tree, not when you add it to the main window.

    use warnings; use strict; use Tk; use Tk::Tree; my $main = MainWindow->new (-title => "Test tree"); my $tree = $main->ScrlTree ( -itemtype => 'text', -separator => '/', -scrollbars => "osoe",-bg => 'red' ); $tree->add ( 1, -text => 'Some sample text', -itemtype => 'text', ); $tree->autosetmode; $tree->close (1); $tree->pack(-fill=>'both',-expand => 1); MainLoop;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      I'm interested in highlighting items in the tree, not the whole thing.

      Thanks anyway.


      Perl is Huffman encoded by design.