pashanoid has asked for the wisdom of the Perl Monks concerning the following question:
You elders are saving lives! :) Here is another one, that is beyond my scope:
In my almost 1k line megaproject I have this area where additional lines of code should appear in my script. I have this Radiobuttons area where if values 1 and 4 are selected more Optionmenus should appear. Is this possible?
my $acc_type_frame = $tab2->Frame()->pack(-side=>'top', -anchor=>'nw',
+ -padx=>'15'); #tab 2
$acc_type_frame->Label(-font => 'tab_rus_bold', -text => '

+58;ип АКБ:')
->pack(-side=>'left'); #define EE_LCD_AccType 0- i
+0;ислотный Р
+077;г, 1- Гелевый/AGM
+ T , 2- Кислотныl
+1; T, //3- Щелочной
$acc_type_frame->Radiobutton(-font => 'tab_rus', -text => '
+050;ислотный Р&
+#1077;г', -value => '0', -variable => \$acc_type, -command =>
+sub {&write_map('384','0')})
->pack(-side=>'left');
$acc_type_frame->Radiobutton(-font => 'tab_rus', -text => '
+043;елевый/AGM T', -value => '1',
+ -variable => \$acc_type, -command => sub {&write_map('384','1')})
->pack(-side=>'left');
$acc_type_frame->Radiobutton(-font => 'tab_rus', -text => '
+050;ислотный Т'
+, -value => '2', -variable => \$acc_type, -command => sub {&write_map
+('384','2')})
->pack(-side=>'left');
$acc_type_frame->Radiobutton(-font => 'tab_rus', -text => '
+065;елочной', -value => '3'
+, -variable => \$acc_type, -command => sub {&write_map('384','3')})
->pack(-side=>'left');
&extra_acc_vals($tab2, $acc_type_frame) if (($acc_type==0 ) ||
+ ($acc_type==3));
This last sub calls the code for more option menus...
Very gratefull to all you!
Re: Dynamically add Optionmenus based on variable perl tk
by Anonymous Monk on Jul 28, 2011 at 09:40 UTC
|
Radiobuttons area where if values 1 and 4 are selected more Optionmenus should appear. Is this possible?
Yes, add event handler, that pops up optionmenu, see
see tk "widget" demo program, example
Menus
3. Menubuttons.
| [reply] |
|
not quite it... see I have an Radio button menu. And if one checks one item then two Option lists should appear below. Other items on the Radiobutton menu shoud not bring up any option list at all...
| [reply] |
Re: Dynamically add Optionmenus based on variable perl tk
by TomDLux (Vicar) on Jul 28, 2011 at 14:57 UTC
|
Nothing to do with your question ...
Looking at the code I see a frightening swarm of indecipherable characters. I would suggest extracting the special codes into predefined variables. That way you have something meaningful, and it becomes easier to verify they contain what you want them to contain.
Readonly my %CHINESE_WORDS =>
( perl => 'Ти+п АКБ:',
monk => 'Ти+п АКБ:'
);
Readonly my %PROTEINS =>
( A => 'Ти+п АКБ:',
C => 'Ти+п АКБ:',
G => 'Ти+п АКБ:',
T => 'Ти+п АКБ:',
);
Obviously I reused the same string over and over, but I think you can see how defining constants would make the strings easier to verify, and the main code shorter and clearer.
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
| [reply] [d/l] |
|
Sorry about that one... I have a Radiobutton with 4 options. If a user selects option 2, everything is fine, a subroutine writes some value to where it supposed to write it and all is well. However, If she selects option 1, well, that subroutine should do the same thing AND another Option menu should appear with more choices because that particular radiobutton needs more info associated with it...
Now, I'm not writting the code for the Optionmenu, presuming the subroutine &extra_acc_vals will generate it. And it does, but I have to select button 1, then close out this window with the Radiobuttons. Then hit a button that opens it again, and THEN these extra Optionmenus appear. I really appreciate your help on this one!!!
my $acc_type_frame = $tab2->Frame()->pack(-side=>'top', -anchor=>'nw',
+ -padx=>'15'); #tab 2
$acc_type_frame->Label(-font => 'tab_rus_bold', -text => 'some
+ text 1') ->pack(-side=>'left');
$acc_type_frame->Radiobutton(-font => 'tab_rus', -text => 'Val
+ue 0 if checked &extra_acc_vals should generate Optionmenus below', -
+value => '0', -variable => \$acc_type, -command => sub {&write_map('
+384','0') #these commands should be executed firs/as well}) ->pack(-s
+ide=>'left');
$acc_type_frame->Radiobutton(-font => 'tab_rus', -text => 'Val
+ue 1 if checked no extra Optionmenus should appear', -value => '1', -
+variable => \$acc_type, -command => sub {&write_map('384','1')})->pac
+k(-side=>'left');
$acc_type_frame->Radiobutton(-font => 'tab_rus', -text => 'Val
+ue 1 if checked no extra Optionmenus should appear', -value => '2', -
+variable => \$acc_type, -command => sub {&write_map('384','2')})
->pack(-side=>'left');
$acc_type_frame->Radiobutton(-font => 'tab_rus', -text => 'Val
+ue 0 if checked &extra_acc_vals should generate Optionmenus below', -
+value => '3', -variable => \$acc_type, -command => sub {&write_map('3
+84','3')})
->pack(-side=>'left');
&extra_acc_vals($tab2, $acc_type_frame) if (($acc_type==0 ) ||
+ ($acc_type==3)); #so if $acc_type is iether 0 or 3 the &extra_acc_va
+ls should generate OptionMenus and such
| [reply] [d/l] |
|
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
my $mw = MainWindow->new;
$mw->geometry("300x300");
my $var =1;
my ($major, $other);
my @options = ('one', 'two', 'three', 'four', 'five');
my $topics1=$mw->Radiobutton(-text =>'Selection 1', -value => '0',
-variable => \$major, -command => sub {&do_smth('1')})->pack(
+);
my $topics2=$mw->Radiobutton(-text =>'Selection 2', -value => '1',
-variable => \$major, -command => sub {&do_smth('2')})->pack()
+;
my $topics3=$mw->Radiobutton(-text =>'Selection 3', -value => '3',
-variable => \$major, -command => sub {&do_smth('3')})->pack()
+;
if ($var eq 'more'){
my $options=$mw->Optionmenu( -options => [@options], -variable
+ => \$other,
-command => [sub {&do_more($major, $other);}])->pack()
+;
}
MainLoop;
sub do_smth{
my $selection = shift;
$var='more' unless ($selection == 2);
}
sub do_more{
my ($major, $other) = @_;
print "do this: $other while Selection was $major\n";
}
How do I make the extra Optionmenu to appear when choice 2 is called?
| [reply] [d/l] |
|
|
|