Thx TGI for this very usefull comment.
I gave it a try, but I still encounter some problems:
- every sublist starts with some separator alike dashes. The number of dashes depends of the number of elements in that sublist
- if I add a 'subelement' to an optionmenu later on, then I cannot check if the 'mainpart' of this element already exist
my code:
#!/usr/local/bin/perl use Tk; # Main Window $mw = new MainWindow; my $var; my $opt = $mw -> Optionmenu(-options => [qw(opt1 opt2 opt3 opt4 dinner +1 dinner2 dinner3 dinner4 desert1 desert2 desert3 desert4 desert5 )], -variable => \$var )->pack; $opt->addOptions('dinner5'); $mw->Button(-text=>'Exit', -command=>sub{$mw->destroy})->pack; MainLoop;
my changed sub addOptions:
sub addOptions { my $w = shift; my $menu = $w->menu; my $tvar = $w->cget(-textvariable); my $vvar = $w->cget(-variable); my $oldt = $$tvar; my $width = $w->cget('-width'); my %hash; my %MainFields; my $first; while (@_) { my $val = shift; my $label = $val; if (ref $val) { if ($vvar == $tvar) { my $new = $label; $w->configure(-textvariable => ($tvar = \$new)); } ($label, $val) = @$val; } my $len = length($label); $width = $len if (!defined($width) || $len > $width); ##### BEGIN CHANGED ####### if ( $label eq '-') { $menu->separator() } elsif ( $label =~ /^(\w+)(\d+)$/ ) { my ($One, $Two)=($1, $2); if ( !defined($MainFields{'DaTa'.$One}) ) { $MainFields{'DaTa'.$One}=$menu->Cascade(-label=>$One); } $MainFields{'DaTa'.$One}->command(-label =>$One.$Two,-command +=> [ $w , 'setOption', $One.$Two ]); } else { $menu->command(-label => $label, -command => [ $w , 'setOption +', $label, $val ]); } ##### END CHANGED ####### # $menu->command(-label => $label, -command => [ $w , 'setOption', +$label, $val ]); $hash{$label} = $val; $first = $label unless defined $first; } if (!defined($oldt) || !exists($hash{$oldt})) { $w->setOption($first, $hash{$first}) if defined $first; } $w->configure('-width' => $width); }

In reply to Re^2: Tk: cascade widget != menu by yike
in thread Tk: cascade widget != menu by yike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.