Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Navigation and Focus - Tk Notebook Widget tabs (UPDATED)

by roho (Chancellor)
on Aug 30, 2020 at 16:33 UTC ( #11121201=perlquestion: print w/replies, xml ) Need Help??

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

UPDATE1: See my first response below for solution to problem #1. Still looking for solution to problem #2.
UPDATE2: Problem #2 was resolved by replacing "Tk::NoteBook" with "Tk::DynaTabFrame", which has the required features, plus much more.

The example code below creates 3 Tk Notebook widget tabs in a Toplevel window. None of the tabs have focus, which means the contents must be clicked on before scrolling can occur. The Notebook tabs can only be navigated by clicking on the tab headers.

My questions are:

  1. How can each Notebook tab be given the focus so they are immediately scrollable without having to click on the contents?
  2. How can the Notebook tabs be navigated by using the keyboard TAB key (and Shift-TAB for reverse navigation)?

TIA for your help.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Menu; use Tk::More; use Tk::NoteBook; my $mw = MainWindow->new(); $mw->geometry("100x100+850+50"); $mw->iconify; my $bw = $mw->Toplevel(); $bw->geometry("850x200+500+200"); $bw->bind('<Destroy>' => sub {exit;}); $bw->bind('<Escape>' => sub {exit;}); my $book = $bw->NoteBook()->pack( -fill=>'both', -expand=>1 ); my $tab1 = $book->add( "Sheet 1", -label=>"Tab #1" ); my $more1 = $tab1->Scrolled("More", -scrollbars => "osoe" )->pack(-fil +l => "both", -expand => 1); my $menu1 = $more1->menu; $bw->configure(-menu => $menu1); $more1->Load( $0 ); my $tab2 = $book->add( "Sheet 2", -label=>"Tab #2" ); my $more2 = $tab2->Scrolled("More", -scrollbars => "osoe" )->pack(-fil +l => "both", -expand => 1); my $menu2 = $more2->menu; $bw->configure(-menu => $menu2); $more2->Load( $0 ); my $tab3 = $book->add( "Sheet 3", -label=>"Tab #3" ); my $more3 = $tab3->Scrolled("More", -scrollbars => "osoe" )->pack(-fil +l => "both", -expand => 1); my $menu3 = $more3->menu; $bw->configure(-menu => $menu3); $more3->Load( $0 ); MainLoop;

"It's not how hard you work, it's how much you get done."

Replies are listed 'Best First'.
Re: Navigation and Focus - Tk Notebook Widget tabs
by roho (Chancellor) on Aug 31, 2020 at 01:09 UTC
    UPDATE -- Fixed the lack of focus problem by calling "$more1->focus;" after "more1->Load( $0 );", and likewise for $more2 and $more3.

    "It's not how hard you work, it's how much you get done."

      I found this by searching for tk gui tab order. I suspect a similar concept may apply here, but not sure. The answer is probably Tk specific since Tk is just an interface to the gui library.
        Thanks for checking perlfan. I realize the stackoverflow post is for Python, but there does not appear to be a single Tk equivalent.

        I have been reading the docs for Tk::NoteBook, Tk::bind, Tk::bindtags, and Tk::callbacks. The answer to navigating Notebook widget tabs with the keyboard TAB key may lie in a combination of these Tk modules, but there are no clear end-to-end examples in the docs.

        "It's not how hard you work, it's how much you get done."

Re: Navigation and Focus - Tk Notebook Widget tabs (UPDATED)
by Anonymous Monk on Sep 04, 2020 at 03:00 UTC

    One

    $book->focus( $book->raised ); $book->focusFollowsMouse;

    or

    $mw->focusFollowsMouse;

    Two

    $book->bind( '<Control-Tab>', \&RaiseNext ); $book->bind( '<Shift-Control-Tab>', \&RaisePrev ); sub RaiseNext { my( $nb ) = @_; $nb->raise( $nb->info("focusnext") ); } sub RaisePrev { my( $nb ) = @_; $nb->raise( $nb->info("focusprev") ); }

    For my win32, Tk::DynaTabFrame doesn't change anything in your program, it actually breaks tab cycling

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2020-12-05 19:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (65 votes). Check out past polls.

    Notices?