Optionmenu(3pm)

✨ Optionmenu β€” Tk::Optionmenu ✨

okay so bestie, this is a User Contributed Perl Documentation page for Tk::Optionmenu, and it's literally the widget that lets your user pick one thing out of a lil set of pre-approved options. dropdown energy. very demure, very mindful. πŸ’…


🌸 NAME 🌸

Tk::Optionmenu</p> β€” let the user select one of some predefined options values πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

use Tk::Optionmenu;

$opt = $w->Optionmenu(
                    -options => REFERENCE_to_OPTIONLIST,
                    -command => CALLBACK,
                    -variable => SCALAR_REF,
                    );

$opt->addOptions( OPTIONLIST );

# OPTION LIST is
#   a)  $val1, $val2, $val3,...
#   b)  [ $lab1=>$val1], [$lab2=>val2], ... ]
#   c)  combination of a) and b), e.g.,
#       val1, [$lab2=>val2], val3, val4, [...], ...

so the option list can be plain values, or label/value pairs in brackets, or a chaotic lil mix of both β€” pick your fighter, all three are valid, no cap πŸ’–


πŸ‘‘ DESCRIPTION πŸ‘‘

The Optionmenu widget lets the user choose between a given set of options. that's it, that's the whole vibe. a curated selection. main character menu energy. ⭐

If you want the user to be able to change the available options themselves, go look at Tk::BrowseEntry instead β€” different widget, different job, iykyk. πŸ¦‹


πŸ’œ OPTIONS πŸ’œ

  • -options β€” (re)sets the list of options that get presented. swapping the whole menu out, slay.
  • -command β€” defines the callback that is invoked when a new option gets selected. it fires, you react. periodt. πŸ”₯
  • -variable β€” a reference to a scalar that holds the current value of the selected option.
  • -textvariable β€” a reference to a scalar that holds the text label of the current value of the selected option. (yes bestie, value and label are two different things here, keep up πŸ’…)

πŸŽ€ METHODS πŸŽ€

addOptions

Adds OPTION_LIST to the options that are already available. stacking new choices on top of the old ones, no restart required. 🌈


🌟 EXAMPLE 🌟

use Tk;
my $mw = MainWindow->new();

my ($var, $tvar);
my $opt = $mw->Optionmenu(
       -options => [[jan=>1], [feb=>2], [mar=>3], [apr=>4]],
       -command => sub { print "got: ", shift, "\n" },
       -variable => \$var,
       -textvariable => \$tvar
      )->pack;

$opt->addOptions([may=>5],[jun=>6],[jul=>7],[aug=>8]);

my $f = $mw->Frame(-relief=>'groove', -borderwidth => 2)->pack;
$f->Label(-textvariable=>\$tvar)->pack(-side => 'left');
$f->Label(-text => " -> ")->pack(-side => 'left');
$f->Label(-textvariable=>\$var)->pack(-side => 'left');

$mw->Button(-text=>'Exit', -command=>sub{$mw->destroy})->pack;

MainLoop;

look at that. months as labels, numbers as values, two whole Labels showing you both at once like a lil split-screen reveal. the ->pack chain is doing the most and honestly it ate. πŸ™ˆ


🫢 SEE ALSO 🫢

Tk::Menubutton, Tk::BrowseEntry


heads up bestie: if your user needs to edit the option list themselves, this is not the widget β€” that's Tk::BrowseEntry's whole thing. don't come for Optionmenu's gig. πŸ’€

sign-off time πŸ’– this page ships with perl v5.40.1, dated 2025-04-13, and it lives in the Optionmenu(3pm) slot. go forth and let people choose things. mwah. πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from