Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Changing or Modifying a Tk Gui on pressing a radiobutton

by Amblikai (Beadle)
on Feb 08, 2013 at 13:11 UTC ( #1017811=perlquestion: print w/ replies, xml ) Need Help??
Amblikai has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks, i was asked to make a gui front end for my recent perl program. I chose perl/Tk which i'm totally new to. However, i was really impressed how quickly i was able to knock something up just by cutting and pasting a few examples i found on the web!

Anyway, the gist of my question is: If i have a couple of radio buttons, how do i set the gui so that part of it is greyed out or (preferably) disappears when one of the radio buttons is pressed?

I've tried searching through examples (there's a lot of good examples) but i can't find any which do this sort of thing! Apologies for the really long post. I hope it's clear. Thanks!

This is the full program/gui:

#!/usr/bin/perl -w use strict; use warnings; use Tk; # Main Window my $mw=new MainWindow; $mw->title("Analog Test Mux Generator"); # Framed Radio buttons at top to choose output #============================================ my $output_select="netlist"; #-------------------------------------------- my $frame=$mw->LabFrame( -label=> "Ouput Select", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); $frame->Radiobutton( -variable=>\$output_select, -value=>'netlist', -text=>'Generate Netlist', ) ->pack(-side=>'left'); $frame->Radiobutton( -variable=>\$output_select, -value=>'template', -text=>'Generate Template', ) ->pack(-side=>'right'); #============================================ my $input_filename; my $sheet_number; my $project_name; #-------------------------------------------- my $netlist_args_frame=$mw->LabFrame( -label=> "Netlist Input Options", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- my $netlist_input_frame=$netlist_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $netlist_input_frame->Label( -text=>'Input Filename:' )->pack(-side=>'left'); $input_filename=$netlist_input_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $netlist_sheet_frame=$netlist_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $netlist_sheet_frame->Label( -text=>'Sheet Name/Number:', )->pack(-side=>'left'); $sheet_number=$netlist_sheet_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $netlist_proj_frame=$netlist_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $netlist_proj_frame->Label( -text=>'Project Name:', )->pack(-side=>'left'); $project_name=$netlist_proj_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- #============================================ my $template_output_filename; my $template_project_name; my $template_pin_list; #-------------------------------------------- my $template_args_frame=$mw->LabFrame( -label=> "Template Input Options", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- my $template_output_frame=$template_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $template_output_frame->Label( -text=>'Template Filename:', )->pack(-side=>'left'); $template_output_filename=$template_output_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $template_proj_frame=$template_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $template_proj_frame->Label( -text=>'Project Name:', )->pack(-side=>'left'); $template_project_name=$template_proj_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $template_pins_frame=$template_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $template_pins_frame->Label( -text=>'Template Pins:', )->pack(-side=>'left'); $template_pin_list=$template_pins_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- #============================================ my $import_working_directory; my $import_ref_lib; my $import_dest_lib; #-------------------------------------------- my $import_args_frame=$mw->LabFrame( -label=> "Import Input Options", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- my $import_dir_frame=$import_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $import_dir_frame->Label( -text=>'Working Directory:', )->pack(-side=>'left'); $import_working_directory=$import_dir_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $import_ref_frame=$import_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $import_ref_frame->Label( -text=>'Reference Library:', )->pack(-side=>'left'); $import_ref_lib=$import_ref_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $import_dest_frame=$import_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $import_dest_frame->Label( -text=>'Destination Library:', )->pack(-side=>'left'); $import_dest_lib=$import_dest_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- #============================================ #============================================ my $merge_option=0; my $spares_option=0; #-------------------------------------------- my $options_frame=$mw->LabFrame( -label=> "Additional Options", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); my $merge_check=$options_frame->Checkbutton( -text=>"Merge Output Controls", -variable=>\$merge_option ) ->pack(-side=>'left'); $merge_check->deselect(); my $spares_check=$options_frame->Checkbutton( -text=>"Add Spare Cells", -variable=>\$spares_option ) ->pack(-side=>'right'); $spares_check->deselect(); #============================================ my $exit = $mw->Button(-text => 'Exit', -command => sub { exit; }); $exit->pack(-side => 'bottom', -expand => '1', -fill => 'x'); # my $generate = $mw->Button(-text => 'Generate', -command => sub { generate(); }); $generate->pack(-side => 'bottom', -expand => '1', -fill => 'x'); MainLoop; #print "Hello World\n"; # subroutines: sub generate { my $input_filename=$input_filename->get; my $project_name=$project_name->get; my $sheet_number=$sheet_number->get; my $template_output_filename=$template_output_filename->get; my $template_project_name=$template_project_name->get; my $template_pin_list=$template_pin_list->get; my $import_working_directory=$import_working_directory->get; my $import_ref_lib=$import_ref_lib->get; my $import_dest_lib=$import_dest_lib->get; print "---------------------------------------------\n"; print "Input = $input_filename\n"; print "Sheet = $sheet_number\n"; print "Project = $project_name\n"; print "---------------------------------------------\n"; print "Template File = $template_output_filename\n"; print "Template Project = $template_project_name\n"; print "Template Pins:\n"; print "$_\n" foreach (split ', ', $template_pin_list); print "---------------------------------------------\n"; print "Working Directory = $import_working_directory\n"; print "Ref Lib = $import_ref_lib\n"; print "Dest Lib = $import_dest_lib\n"; print "---------------------------------------------\n"; } __END__

I'd like when pressing the "Netlist" radio button at the top, for the "template" input fields do dissappear.

Then when pressing "Template" radibutton, the opposite, so only the "template" input fields would be present (and of course, the 2 buttons in both cases)

So the gui for the netlist radio button having been pressed would look like:

#!/usr/bin/perl -w use strict; use warnings; use Tk; # Main Window my $mw=new MainWindow; $mw->title("Analog Test Mux Generator"); # Framed Radio buttons at top to choose output #============================================ my $output_select="netlist"; #-------------------------------------------- my $frame=$mw->LabFrame( -label=> "Ouput Select", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); $frame->Radiobutton( -variable=>\$output_select, -value=>'netlist', -text=>'Generate Netlist', ) ->pack(-side=>'left'); $frame->Radiobutton( -variable=>\$output_select, -value=>'template', -text=>'Generate Template', ) ->pack(-side=>'right'); #============================================ my $input_filename; my $sheet_number; my $project_name; #-------------------------------------------- my $netlist_args_frame=$mw->LabFrame( -label=> "Netlist Input Options", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- my $netlist_input_frame=$netlist_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $netlist_input_frame->Label( -text=>'Input Filename:' )->pack(-side=>'left'); $input_filename=$netlist_input_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $netlist_sheet_frame=$netlist_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $netlist_sheet_frame->Label( -text=>'Sheet Name/Number:', )->pack(-side=>'left'); $sheet_number=$netlist_sheet_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $netlist_proj_frame=$netlist_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $netlist_proj_frame->Label( -text=>'Project Name:', )->pack(-side=>'left'); $project_name=$netlist_proj_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- #============================================ #my $template_output_filename; #my $template_project_name; #my $template_pin_list; ##-------------------------------------------- #my $template_args_frame=$mw->LabFrame( #-label=> "Template Input Options", #-labelside=>'acrosstop', #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #my $template_output_frame=$template_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$template_output_frame->Label( #-text=>'Template Filename:', #)->pack(-side=>'left'); #$template_output_filename=$template_output_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- #my $template_proj_frame=$template_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$template_proj_frame->Label( #-text=>'Project Name:', #)->pack(-side=>'left'); #$template_project_name=$template_proj_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- #my $template_pins_frame=$template_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$template_pins_frame->Label( #-text=>'Template Pins:', #)->pack(-side=>'left'); #$template_pin_list=$template_pins_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- #============================================ my $import_working_directory; my $import_ref_lib; my $import_dest_lib; #-------------------------------------------- my $import_args_frame=$mw->LabFrame( -label=> "Import Input Options", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- my $import_dir_frame=$import_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $import_dir_frame->Label( -text=>'Working Directory:', )->pack(-side=>'left'); $import_working_directory=$import_dir_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $import_ref_frame=$import_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $import_ref_frame->Label( -text=>'Reference Library:', )->pack(-side=>'left'); $import_ref_lib=$import_ref_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $import_dest_frame=$import_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $import_dest_frame->Label( -text=>'Destination Library:', )->pack(-side=>'left'); $import_dest_lib=$import_dest_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- #============================================ #============================================ my $merge_option=0; my $spares_option=0; #-------------------------------------------- my $options_frame=$mw->LabFrame( -label=> "Additional Options", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); my $merge_check=$options_frame->Checkbutton( -text=>"Merge Output Controls", -variable=>\$merge_option ) ->pack(-side=>'left'); $merge_check->deselect(); my $spares_check=$options_frame->Checkbutton( -text=>"Add Spare Cells", -variable=>\$spares_option ) ->pack(-side=>'right'); $spares_check->deselect(); #============================================ my $exit = $mw->Button(-text => 'Exit', -command => sub { exit; }); $exit->pack(-side => 'bottom', -expand => '1', -fill => 'x'); # my $generate = $mw->Button(-text => 'Generate', -command => sub { generate(); }); $generate->pack(-side => 'bottom', -expand => '1', -fill => 'x'); MainLoop; #print "Hello World\n"; # subroutines: sub generate { my $input_filename=$input_filename->get; my $project_name=$project_name->get; my $sheet_number=$sheet_number->get; #my $template_output_filename=$template_output_filename->get; #my $template_project_name=$template_project_name->get; #my $template_pin_list=$template_pin_list->get; my $import_working_directory=$import_working_directory->get; my $import_ref_lib=$import_ref_lib->get; my $import_dest_lib=$import_dest_lib->get; print "---------------------------------------------\n"; print "Input = $input_filename\n"; print "Sheet = $sheet_number\n"; print "Project = $project_name\n"; #print "---------------------------------------------\n"; #print "Template File = $template_output_filename\n"; #print "Template Project = $template_project_name\n"; #print "Template Pins:\n"; #print "$_\n" foreach (split ', ', $template_pin_list); print "---------------------------------------------\n"; print "Working Directory = $import_working_directory\n"; print "Ref Lib = $import_ref_lib\n"; print "Dest Lib = $import_dest_lib\n"; print "---------------------------------------------\n"; } __END__

and the "Template" Radio button pressed would look like this:

#!/usr/bin/perl -w use strict; use warnings; use Tk; # Main Window my $mw=new MainWindow; $mw->title("Analog Test Mux Generator"); # Framed Radio buttons at top to choose output #============================================ my $output_select="template"; #-------------------------------------------- my $frame=$mw->LabFrame( -label=> "Ouput Select", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); $frame->Radiobutton( -variable=>\$output_select, -value=>'netlist', -text=>'Generate Netlist', ) ->pack(-side=>'left'); $frame->Radiobutton( -variable=>\$output_select, -value=>'template', -text=>'Generate Template', ) ->pack(-side=>'right'); #============================================ #my $input_filename; #my $sheet_number; #my $project_name; ##-------------------------------------------- #my $netlist_args_frame=$mw->LabFrame( #-label=> "Netlist Input Options", #-labelside=>'acrosstop', #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #my $netlist_input_frame=$netlist_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$netlist_input_frame->Label( #-text=>'Input Filename:' #)->pack(-side=>'left'); #$input_filename=$netlist_input_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- #my $netlist_sheet_frame=$netlist_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$netlist_sheet_frame->Label( #-text=>'Sheet Name/Number:', #)->pack(-side=>'left'); #$sheet_number=$netlist_sheet_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- #my $netlist_proj_frame=$netlist_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$netlist_proj_frame->Label( #-text=>'Project Name:', #)->pack(-side=>'left'); #$project_name=$netlist_proj_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- #============================================ my $template_output_filename; my $template_project_name; my $template_pin_list; #-------------------------------------------- my $template_args_frame=$mw->LabFrame( -label=> "Template Input Options", -labelside=>'acrosstop', ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- my $template_output_frame=$template_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $template_output_frame->Label( -text=>'Template Filename:', )->pack(-side=>'left'); $template_output_filename=$template_output_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $template_proj_frame=$template_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $template_proj_frame->Label( -text=>'Project Name:', )->pack(-side=>'left'); $template_project_name=$template_proj_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- my $template_pins_frame=$template_args_frame->Frame( ) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); #-------------------------------------------- $template_pins_frame->Label( -text=>'Template Pins:', )->pack(-side=>'left'); $template_pin_list=$template_pins_frame->Entry( # Width is in characters not in pixels -width=>35, )->pack(-side=>'right'); #-------------------------------------------- ##============================================ #my $import_working_directory; #my $import_ref_lib; #my $import_dest_lib; ##-------------------------------------------- #my $import_args_frame=$mw->LabFrame( #-label=> "Import Input Options", #-labelside=>'acrosstop', #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #my $import_dir_frame=$import_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$import_dir_frame->Label( #-text=>'Working Directory:', #)->pack(-side=>'left'); #$import_working_directory=$import_dir_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- #my $import_ref_frame=$import_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$import_ref_frame->Label( #-text=>'Reference Library:', #)->pack(-side=>'left'); #$import_ref_lib=$import_ref_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- #my $import_dest_frame=$import_args_frame->Frame( #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); ##-------------------------------------------- #$import_dest_frame->Label( #-text=>'Destination Library:', #)->pack(-side=>'left'); #$import_dest_lib=$import_dest_frame->Entry( ## Width is in characters not in pixels #-width=>35, #)->pack(-side=>'right'); ##-------------------------------------------- ##============================================ ##============================================ #my $merge_option=0; #my $spares_option=0; ##-------------------------------------------- #my $options_frame=$mw->LabFrame( #-label=> "Additional Options", #-labelside=>'acrosstop', #) ->pack(-expand=> '1', -fill=>'both', -side=>'top'); # #my $merge_check=$options_frame->Checkbutton( #-text=>"Merge Output Controls", #-variable=>\$merge_option #) ->pack(-side=>'left'); #$merge_check->deselect(); # #my $spares_check=$options_frame->Checkbutton( #-text=>"Add Spare Cells", #-variable=>\$spares_option #) ->pack(-side=>'right'); #$spares_check->deselect(); # #============================================ my $exit = $mw->Button(-text => 'Exit', -command => sub { exit; }); $exit->pack(-side => 'bottom', -expand => '1', -fill => 'x'); # my $generate = $mw->Button(-text => 'Generate', -command => sub { generate(); }); $generate->pack(-side => 'bottom', -expand => '1', -fill => 'x'); MainLoop; #print "Hello World\n"; # subroutines: sub generate { #my $input_filename=$input_filename->get; #my $project_name=$project_name->get; #my $sheet_number=$sheet_number->get; my $template_output_filename=$template_output_filename->get; my $template_project_name=$template_project_name->get; my $template_pin_list=$template_pin_list->get; #my $import_working_directory=$import_working_directory->get; #my $import_ref_lib=$import_ref_lib->get; #my $import_dest_lib=$import_dest_lib->get; #print "---------------------------------------------\n"; #print "Input = $input_filename\n"; #print "Sheet = $sheet_number\n"; #print "Project = $project_name\n"; print "---------------------------------------------\n"; print "Template File = $template_output_filename\n"; print "Template Project = $template_project_name\n"; print "Template Pins:\n"; print "$_\n" foreach (split ', ', $template_pin_list); print "---------------------------------------------\n"; #print "Working Directory = $import_working_directory\n"; #print "Ref Lib = $import_ref_lib\n"; #print "Dest Lib = $import_dest_lib\n"; #print "---------------------------------------------\n"; } __END__

Sorry for the huge post i tried to make it as clear as possible! Thanks for any help you could provide.

Comment on Changing or Modifying a Tk Gui on pressing a radiobutton
Select or Download Code
Re: Changing or Modifying a Tk Gui on pressing a radiobutton
by tmharish (Friar) on Feb 08, 2013 at 14:03 UTC
    Apologies for the really long post.
    Sorry for the huge post i tried to make it as clear as possible!

    I think its brilliant that you have put down as much as you have - Clear answers are possible only when the question is clear.

    However its generally a good idea to use readmore tags.

      Thanks, i didn't know about those, i'll be sure to use them in my next post!

Re: Changing or Modifying a Tk Gui on pressing a radiobutton
by thundergnat (Deacon) on Feb 08, 2013 at 18:07 UTC

    Maybe this will give you some useful hints.

    use strict; use warnings; use Tk; my %w; #widget hash $w{mw} = MainWindow->new; my $state = 'normal'; $w{fr} = $w{mw}->Frame()->pack( -expand => '1', -fill => 'x', -side => 'top' ); for ( 'normal', 'disabled', 'invisible' ) { $w{fr}->Radiobutton( -variable => \$state, -value => $_, -text => ucfirst $_, -command => sub { change( $state, $w{fr2} ) } )->pack( -side => 'left' ); } $w{fr2} = $w{mw}->Frame()->pack( -expand => '1', -fill => 'both', -side => 'top' ); for ( 1 .. 10 ) { $w{fr2}->Label( -text => "Label $_" )->grid( -row => $_, -column = +> 0 ); $w{fr2}->Entry()->grid( -row => $_, -column => 1 ); } MainLoop; sub change { my ( $state, $widget ) = @_; if ( $state eq 'invisible' ) { $widget->packForget } else { $widget->pack; $_->configure( -state => $state ) for $widget->children; } }
Re: Changing or Modifying a Tk Gui on pressing a radiobutton
by golux (Pilgrim) on Feb 08, 2013 at 19:24 UTC
    Hi Amblikai,

    Here's how you could create a data structure defining regions to reveal/hide when the corresponding RadioButton is pressed.

    First, assign variables to your RadioButtons. For example:

    my $rb1 = $frame->Radiobutton( -variable=>\$output_select, -value=>'netlist', -text=>'Generate Netlist', ) ->pack(-side=>'left'); my $rb2 = $frame->Radiobutton( -variable=>\$output_select, -value=>'template', -text=>'Generate Template', ) ->pack(-side=>'right');
    Later in the program, after you've defined ALL the widgets which you want to hide/reveal (but before the MainLoop), create your data structure:
    # Data structure to control 'hideable' regions # # Each key is a valid RadioButton. Each value is a list of widgets # to REVEAL when that RadioButton is clicked, at which time all other # widgets ((in other Array Refs) will be hidden. ## my $a_reveal = [ [ $rb1 => [ $netlist_args_frame, $import_args_frame, $options_fram +e ] ], [ $rb2 => [ $template_args_frame ] ], ]; assign_hideable_widgets($a_reveal);
    Finally, in your subroutine section, create the subroutine which sets up the callbacks for each RadioButton, to hide or reveal the specific widgets (in this case, they're all LabFrames):
    sub assign_hideable_widgets { my ($a_reveal) = @_; # Return if no hideable regions defined @$a_reveal or return; # Save all hideable regions my $a_all = [ ]; # List of all widgets my $h_seen = { }; # Hash all widgets seen my $h_reveal = { }; # Maps RadioButton to widgets to reveal my $h_packinfo = { }; # Saves each widget's pack information # Create closure to hide/reveal desired regions my $c_hide = sub { my ($rb) = @_; # First, unpack everything map { $_->packForget } @$a_all; # Next, pack selected widgets for this RadioButton my $a_reveal = $h_reveal->{$rb}; foreach my $w (@$a_reveal) { my $a_pack = $h_packinfo->{$w}; $w->pack(@$a_pack); } }; # Iterate each RadioButton and the widget(s) it reveals foreach my $a_item (@$a_reveal) { my ($rb, $a_reveal) = @$a_item; $h_reveal->{$rb} = $a_reveal; # Save all widgets and their pack info foreach my $w (@$a_reveal) { $h_seen->{$w}++ or push @$a_all, $w; $h_packinfo->{$w} = [ $w->packInfo ]; } $rb->bind("<Button-1>" => sub { $c_hide->($rb) }); } }

    say  substr+lc crypt(qw $i3 SI$),4,5

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017811]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-18 01:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls