in reply to Re^2: A Perl Tk::NoteBook question
in thread A Perl Tk::NoteBook question

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

Replies are listed 'Best First'.
Re^4: A Perl Tk::NoteBook question
by Anonymous Monk on Jul 10, 2014 at 06:10 UTC


    Hi Zentara,
    Thank you for those two possible solutions. It took me sometime to go through your code as I'm new to Perl Tk. Although I haven't understood fully the Pixmap things in great detail, I'll try to check if there is any docs on it (the Tk docs on this is not detailed enough for newbie like me)

    I think, it would have been much better if there was a way to add image (say a tick-mark) before the text string on the tab only when that page is raised and remove the image. But I realize that the 'add' and 'pageconfigure' methods in docs says that -image option is used only when -label is not specified.

    Therefore I'll have to stick to the -background and -inactivebackground options (which you specified in this response)to aesthetically distinguish between the active page and the inactive ones.
    Thanks again!

      I would look for finding a way to put an actual Label in the tab, then all you need to do is swap lables in and out. The Tk widgets are a simple toolkit, not designed for all the fancy stuff, like Gtk2 and Gtk3 are. Gtk will allow you to put Pango ( stylized text) into any Label, and I may be wrong, but you can add the Label to the tab.

      The -label option in Tk::NoteBook dosn't refer to an actual Label widget, but it would be nice if it did.


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

        Simple toolkit is good as my intention was more to learn a simple GUI in Perl and not worry too much about styles.
        I'm fine with Tk as it has been around for a long time and fulfills it's functions quite adequately (like you said in one your earlier posts: "in praise of poor old Tk").

        Anyway, I must again thank you for all your quick responses in this regard.