Menu::Item(3pm)

okay bestie, welcome to the Menu::Item(3pm) glow-up. it's Perl, it's Tk, it's giving user contributed perl documentation energy, and we're gonna read it together. grab a snack. πŸ“


πŸ’– NAME πŸ’–

Tk::Menu::Item β€” the base class for Menu items. yes, the mother of all menu items. the blueprint. the blueprint that ate. πŸ‘‘


🌸 SYNOPSIS 🌸

here's the vibe, verbatim, no cap:

require Tk::Menu::Item;

my $but = $menu->Button(...);
$but->configure(...);
my $what = $but->cget();

package Whatever;
require Tk::Menu::Item;
@ISA = qw(Tk::Menu::Item);

sub PreInit
{
 my ($class,$menu,$info) = @_;
 $info->{'-xxxxx'} = ...
 my $y = delete $info->{'-yyyy'};
}

see how she just... exists? iconic. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

Tk::Menu::Item is literally the base class, and these babies are all derived from her:

  • Tk::Menu::Button
  • Tk::Menu::Cascade
  • Tk::Menu::Radiobutton
  • Tk::Menu::Checkbutton

and there's also a Tk::Menu::Separator hanging out in the group chat too, don't forget her. 🫢

Constructors are declared so that stuff like $menu->Button(...) and friends just do what you would expect. no surprises, no plot twists, just menu items behaving. ✨


πŸŽ€ the -label pre-processing moment

the -label option gets pre-processed, which lets you slap a ~ in front of a character to derive a -underline value. so like:

$menu->Button(-label => 'Goto ~Home',...)

is equivalent to:

$menu->Button(-label => 'Goto Home', -underline => 6, ...)

same thing bestie, just the tilde did the work for you. the way that little ~ carries the whole operation... πŸ˜­πŸ’–


🌈 the Cascade menu item

the Cascade menu item creates a sub-menu, and she accepts these options:

  • -menuitems β€” a list of items for the sub-menu. within this list (which is also accepted by Menu and Menubutton) the first two elements of each item should be the constructor name and the label:
-menuitems => [
               [Button      => '~Quit', -command => [destroy => $mw]],
               [Checkbutton => '~Oil',  -variable => \$oil],
              ]
  • -postcommand β€” a callback to be invoked before posting the menu. she calls ahead. so polite. πŸ“ž
  • -tearoff β€” specifies whether the sub-menu can be torn-off or not.
  • -menuvar β€” a scalar reference that will be set to the newly-created sub-menu. she gets the new baby's address, fr fr.

πŸ’« what you actually get back

the returned object is currently a blessed reference to an array of two items:

  1. the containing Menu
  2. the 'label'

and the methods configure and cget are mapped onto the underlying entryconfigure and entrycget. so when you poke one, the other one feels it. iykyk. πŸ™ˆ


πŸ‘‘ the whole point of the OO interface

the main purpose of the OO interface is to allow derived item classes to be defined which pre-set the options used to create a more basic item. like, you make a fancy little subclass and she already knows what she's about. main character energy, genuinely. ⭐


πŸ’€ BUGS πŸ’€

okay so, full transparency, no gatekeeping:

this OO interface is very new. 🍼

using the label as the "key" is a problem for separaror items which don't have one. the alternative would be to use an index into the menu, but that is a problem if items are deleted (or inserted other than at the end). so basically: both options are a little cursed. pick your poison. 😭

there should probably be a PostInit entry point too, or a more widget like defered 'configure'. noted, filed, manifesting it. 🧚


and that's the whole page, bestie!! πŸ’– this was documented in perl v5.40.1, dated 2025-04-13, and it's the Menu::Item(3pm) page β€” so go forth and build menus, and remember: the tilde does the underlining for you, and you never have to count characters yourself. slay. πŸ’…βœ¨πŸŒˆ

πŸ“¦ where this page comes from