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

The SSCCE below:

The basic menu contains default settings (for example: "View->Wrap->Character"). How do I change default settings in the basic menu? I need the basic menu default to be "View->Wrap->Word".
TIA for any help you can provide.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Menu; use Tk::More; use Tk::NoteBook; my $mw = new MainWindow; $mw->geometry("100x100+0+0"); my $bw = $mw->Toplevel(); $bw->geometry("400x200"); my $book = $bw->NoteBook()->pack( -fill=>'both', -expand=>1 ); my $tab = $book->add( "Sheet 1", -label=>"Tab #1" ); my $more = $tab->Scrolled("More", -scrollbars => "osoe",)->pack(-fill +=> "both", -expand => 1); ##################################### # Add basic menu to Toplevel window. ##################################### my $menu = $more->menu; $bw->configure(-menu => $menu); MainLoop;

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

Replies are listed 'Best First'.
Re: How to Change Defaults in Basic Tk Menu
by choroba (Cardinal) on Aug 29, 2020 at 22:20 UTC
    Imitate a click on the radio button before starting the main loop:
    my $wrap = $menu->entrycget(3, '-menu')->entrycget(3, '-menu'); $wrap->invoke(0);

    Update: Another option is to set the associated variable to the expected value:

    my $wrap = $menu->entrycget(3, '-menu')->entrycget(3, '-menu'); my $word = $wrap->entrycget(0, '-value'); # 'word' ${ $wrap->entrycget(0, '-variable') } = $word;
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Note that for readability you can also use the labels instead of numerical indices:

      my $wrap = $menu->entrycget('View','-menu')->entrycget('Wrap','-menu') +; $wrap->invoke('Word');

      @roho: I don't know how choroba did it, but I found out that you've been using a method $more->menu and failed to find documentation for it. This made me curious, and I found that this method gets you the hardcoded menu from the Tk::Text widget, only that you don't get a chance to set the -wrap option for this widget. The rest was a matter of digging through this menu and the Tk docs...

        Thanks for the follow-up info haj. This was indeed quite obscure.

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

      Thank you choroba! I used your second option. It works perfectly. I searched but could not find anything remotely resembling the steps you laid out. How on earth (and from where) did you dig out those details? Thanks again.

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

        Experience with Tk, documentation of Tk::Menu, plus Data::Dumper and a few lines like
        print Dumper($menu->entrycget(0, '-menu')); print Dumper($menu->entrycget(0, '-menu')->entrycget(0, '-label'));

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]