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

Hello Perl Folks,

I'm interested to know,

(A) if it's possible in Perl Tk to change the font of the text string displayed on page-tab of Tk::NoteBook whenever that particular page is raised. i.e. in comparison to other page tabs the currently raised page should have it's text string displayed in bold as well as underlined.

(B) Also is it possible to change the foreground color of this text string (to say 'blue') only when raised ?

Here's a sample code I've written.

I guess the problem I'm facing is, the NoteBook object is of type Tk::Widget while the page tabs are of type Tk::Frame and the text string in page tab is not a Tk::Label object so that I can change it's font and color. The text string in the page tab is created via the -label option in Tk::NoteBook widget and I'm unable to get a handle on it so that I can change it's font and color.
Any solution or workaround will be helpful.
PS: I'm using Tk version: 804.032 on Windows XP

use Tk; use Tk::NoteBook; my $mw = MainWindow->new; $mw->geometry('300x200'); my $page_tab_default_font = [-family =>'Linotype Birka', -size => 15, -weight => 'normal', -underline => 0]; my $page_tab_select_font = [-family =>'Linotype Birka', -size => 15, -weight => 'bold', -underline => 1]; my $nb_widget = $mw->NoteBook(-font=>$page_tab_default_font) ->pack(-expand=>1, -fill=>'both'); my $nb_page_01= $nb_widget->add("page1", -label=>'PAGE 1', -raisecmd=>\&raised_handler); my $nb_page_02=$nb_widget->add("page2", -label=>'PAGE 2', -raisecmd=>\&raised_handler); MainLoop; sub raised_handler { my $raised_page = $nb_widget->page_widget($nb_widget->raised()); # # HOW DO I PROCEED FURTHER ????? # }

Replies are listed 'Best First'.
Re: A Perl Tk::NoteBook question
by zentara (Cardinal) on Jul 09, 2014 at 14:50 UTC
    You just need to configure your notebook and tab to use the colors you want. You have to be careful what frame or widget is in the Notebook frame. For example, if you have a canvas in there, you need to change the canvas color. I've used this technique to match tab and notebook page colors in ztkdb where you can see how I juggle a hash full of colors, to make the tab and frame have the same color. You can have a hash giving values like $nb_color{$page} = 'whatever'; and load it when $page is raised.

    A simpler example:

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::NoteBook; my $mw = MainWindow->new; $mw->geometry('300x200'); my $page_tab_default_font = [-family =>'Linotype Birka', -size => 15, -weight => 'normal', -underline => 0]; my $page_tab_select_font = [-family =>'Linotype Birka', -size => 15, -weight => 'bold', -underline => 1]; my $nb_widget = $mw->NoteBook(-font=>$page_tab_default_font) ->pack(-expand=>1, -fill=>'both'); my $nb_page_01= $nb_widget->add("page1", -label=>'PAGE 1', -raisecmd=>\&raised_handler); my $nb_page_02=$nb_widget->add("page2", -label=>'PAGE 2', -raisecmd=>\&raised_handler); MainLoop; sub raised_handler { my $raised_page = print "@_\n"; my $frame = shift; print "current frame: $frame\n"; my $current = $nb_widget->raised(); print "$current is current\n"; #$nb_widget->page_widget($nb_widget->raised()); # # HOW DO I PROCEED FURTHER ????? # $frame->configure(-bg=> 'lightyellow'); $nb_widget->configure(-bg => 'lightyellow'); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Hello Zentara,

      Before posting my question, I tried to look around in perlmonks to check if this was question already answered. But I could not find it. However, I did notice that you take very much interest in answering Perl Tk questions. So I was expecting you would answer my question too. And there you're!

      Now, to go back to my question, what I was really interested in was to change the font and color of the text string in the page-tab. i.e. The text string "PAGE 1" when page1 is raised should be bold and underlined AND this string should be displayed in a different color (say blue) than it's default (which is black when it is not raised).

      Any thoughts on how to do this ?

      BTW, I'll take a look at your ztkdb but I must confess I'm new to Perl Tk. So I'm not sure how much I'll be able to understand.

        Yeah, don't bother with ztkdb, it will just confuse you. ;-)

        Here are a few tricks to play with. One is adding an Image to your tab, and the other is manipulating the tab itelf. I havn't looked at the docs in awhile, but you can configure a font in my previous example. I don't know if you can configure a font to change in the tab, as it might mess up the default spacing. You can make custom raised and non_raised images for each page and swap them around in the raise command, by looping thru a hash of page colors.

        #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::NoteBook; my $mw=MainWindow->new(-title=>'Notebook tab colors demo'); my $XIMG = <<'EOF'; /* XPM */ static char * x_xpm[] = { "12 12 3 1", " c None", ". c #FF6060", "+ c #8F6060", " . . ", " .+. .+. ", ".+++. .+++. ", "+.+++.+++.+ ", " +.+++++.+ ", " +.+++.+ ", " .+++++. ", " .+++.+++. ", ".+++.+.+++. ", "+.+.+ +.+.+ ", " +.+ +.+ ", " + + "}; EOF my $RED = $mw->Pixmap(-data => $XIMG ); my $BXIMG = $XIMG; $BXIMG =~ s/#FF6060/#6060FF/; $BXIMG =~ s/#8F6060/#60608F/; my $BLUE = $mw->Pixmap(-data => $BXIMG ); my $GXIMG = $XIMG; $GXIMG =~ s/#FF6060/#60FF60/; $GXIMG =~ s/#8F6060/#60FF60/; my $GREEN = $mw->Pixmap(-data => $GXIMG ); my $nb = $mw->NoteBook( -background=> 'white', -inactivebackground => 'black', )->pack(); my @colors = ($RED,$BLUE,$GREEN); for my $tab (qw/page1 page2 page3 page4 page5 page6/){ my $frame = $nb->add($tab, -image=> $colors[0] ); $frame->Label(-text => $tab)->pack; $frame->Label(-text => 'a Label in '.$tab)->pack; push (@colors,shift(@colors)); #shift colors around } MainLoop;
        And a blinking tab!
        #!/usr/bin/perl use Tk; use Tk::NoteBook; use Tk::Photo; use Tk::Compound; use strict; my $mw=tkinit; my @tabs = qw/One Two Three/; my $nb=$mw->NoteBook( -font => "Arial 24", -background=>'lightgreen', -inactivebackground => "lightgray", -tabpadx => 5, -tabpady => 5, )->pack(-expand=>1, -fill=>'both'); my $c = $mw->Compound(-bg=>'red',-showbackground=>1); $c->Text(-text => "Four", -font => "Arial 24"); foreach my $title (@tabs){ $nb->add($title,-label=>$title); } $nb->add("Four",-image => $c); my $id = $mw->repeat(500,sub{&chgtab($tabs[3])}); $nb->bind('<ButtonRelease-1>', [\&killtimer,Ev('x'),Ev('y')]); MainLoop; sub killtimer{ my ($w,$x,$y) = @_; my $tab = $w->identify($x,$y); if ($tab eq "Four"){ $nb->pageconfigure($tab,-label=>$tab); $c->configure(showbackground=>0); $id->cancel; } } sub chgtab { $c->configure(-showbackground=>!$c->cget(-showbackground)); } __END__

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh