graff has asked for the wisdom of the Perl Monks concerning the following question:
Of course, when the user actually clicks on the Optionmenu widget and selects one of the current menu items, this runs the callback with the menu value chosen by the user, and this is fine.
My problem is, how can the callback function tell the difference between being invoked by an actual menu selection (user has made a choice, so do something with that) vs. being invoked because the options have been changed (user hasn't really picked a specific option, so don't do anything yet).
Here's a little test script demonstrates the behavior: when the window first starts up, the first installation of options invokes the callback. Thereafter, the callback is invoked when the Optionmenu itself is used to make a selection, and each time the "Change Options" button is clicked.
I want the callback to be able to skip its work (or not be called in the first place) when installing a new menu. How do I do that?
#!/usr/bin/perl use strict; use Tk; my $mw = MainWindow->new(); my ($var, $tvar); my $opt = $mw->Optionmenu( -command => \&show_choice, -variable => \$var, -textvariable => \$tvar )->pack; change_ops( $opt ); 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=>'Change Options',-command=>[\&change_ops, $opt])->p +ack; $mw->Button(-text=>'Exit', -command=>sub{$mw->destroy})->pack; MainLoop; sub show_choice { print "got: ", shift, "\n" } sub change_ops { my $op = shift; my @newoptions = map { ++$. } ( 0 .. 3 ); $op->configure( -options => \@newoptions ); }
I've looked at the source (Optionmenu.pm), and I could create a modified version of this widget, altering its "addOptions" and "setOption" functions so that the latter does not invoke the callback whenever it happens to be called by the former...
But creating a new version of a widget is the sort of thing I'm likely to do wrong the first few times I try, so I'm hoping some monk can guide me (actually, I'm hoping there's an easier work-around that I'm missing).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Setting options in Tk::Optionmenu without the callback?
by BrowserUk (Patriarch) on Aug 22, 2007 at 23:08 UTC | |
|
Re: Setting options in Tk::Optionmenu without the callback?
by GrandFather (Saint) on Aug 22, 2007 at 23:18 UTC | |
|
Re: Setting options in Tk::Optionmenu without the callback?
by zentara (Cardinal) on Aug 23, 2007 at 12:19 UTC | |
by graff (Chancellor) on Aug 23, 2007 at 15:46 UTC | |
|
Re: Setting options in Tk::Optionmenu without the callback?
by Anonymous Monk on Apr 30, 2014 at 18:19 UTC |