Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Get value from Option menu in perl TK and set default value for option.

by Ganesh Bharadwaj1 (Sexton)
on Aug 03, 2016 at 07:23 UTC ( [id://1169067]=perlquestion: print w/replies, xml ) Need Help??

Ganesh Bharadwaj1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I want to print the options selected by the user in the text box after the push button is pressed. Default options for the option menu needs to be none and the default option for the entry menu needs to be " ". I am not sure how to set the default option. Once the user selects a new option and presses Go! button I want those values to be retrieved and printed inside the push button function. I am not sure how to print the values that are got by selecting from the optionsmenu.

So the two major questions are

1. How to set a default value to the options menus and the entry menus.

2. How to get the value from the options menu once push button is pressed and print it in the text box.

Kindly let me know how to modify my code :)

use Tk; use Tk::widgets qw(LabFrame); use File::Spec::Functions qw(canonpath); use Tk::Optionmenu; my $mw = MainWindow->new; #Entering the metal stack options and metal lines my $metaloption_lf2 = $mw->LabFrame( # -label => 'XY Co-Ordinates and Metal to trace', # -labelside => 'acrosstop', # -foreground => 'blue', ); $metaloption_lf2->pack( -expand => 0, -fill => 'x', ); my $frm_metalOption2 =$metaloption_lf2 ->Frame()->pack; # just for cen +tering the buuton row ##########################################Co-ordinate and metal option +: Row 2 #################################################### my $labx2 = $metaloption_lf2 -> Label(-text=>"X2:")-> grid(-row=>1,-co +lumn=>0)->pack( -side => 'left' ); my $x2 = $metaloption_lf2->Entry( -width => 15, )->grid( -row => 1, -column => 1, -padx => 5, -pady => 5, )->pack( -side => 'left' ); my $laby2 = $metaloption_lf2 -> Label(-text=>"Y2:")-> grid(-row=>1,-co +lumn=>2)->pack( -side => 'left' ); my $y2 = $metaloption_lf2->Entry( -width => 15, )->grid( -row => 1, -column => 3, -padx => 5, -pady => 5, )->pack( -side => 'left' ); my $labmetal2 = $metaloption_lf2 -> Label(-text=>"Metal:")-> grid(-row +=>1,-column=>4)->pack( -side => 'left' ); my $opt2 = $metaloption_lf2->Optionmenu( -options => [[M1=>1], [M2=>2], [M3=>3], [M4=>4]], #-command => sub { print "got: ", shift, "\n" }, #-command => sub { get() }, -variable => \$var, -textvariable => \$tvar )->pack(-side => 'left', -anchor => 'n',); # populate with some values unless done during initialisation $opt2->addOptions([M5=>5],[M6=>6],[M7=>7],[M8=>8],[M9=>9],[M10=>10], [ +NONE=>" "]); $tvar = $default_var; #set default $opt2-> configure( -textvariable => \$tvar ); ###################################################################### +############################################################ #Entering the metal stack options and metal lines my $metaloption_lf3 = $mw->LabFrame( # -label => 'XY Co-Ordinates and Metal to trace', # -labelside => 'acrosstop', # -foreground => 'blue', ); $metaloption_lf3->pack( -expand => 0, -fill => 'x', ); my $frm_metalOption3 =$metaloption_lf3 ->Frame()->pack; # just for cen +tering the buuton row ##########################################Co-ordinate and metal option +: Row 3 #################################################### my $labx3 = $metaloption_lf3 -> Label(-text=>"x3:")-> grid(-row=>1,-co +lumn=>0)->pack( -side => 'left' ); my $x3 = $metaloption_lf3->Entry( -width => 15, )->grid( -row => 1, -column => 1, -padx => 5, -pady => 5, )->pack( -side => 'left' ); my $laby3 = $metaloption_lf3 -> Label(-text=>"y3:")-> grid(-row=>1,-co +lumn=>2)->pack( -side => 'left' ); my $y3 = $metaloption_lf3->Entry( -width => 15, )->grid( -row => 1, -column => 3, -padx => 5, -pady => 5, )->pack( -side => 'left' ); my $labmetal3 = $metaloption_lf3 -> Label(-text=>"Metal:")-> grid(-row +=>1,-column=>4)->pack( -side => 'left' ); my $opt3 = $metaloption_lf3->Optionmenu( -options => [[M1=>1], [M2=>2], [M3=>3], [M4=>4]], #-command => sub { print "got: ", shift, "\n" }, #-command => sub { get() }, -variable => \$var, -textvariable => \$tvar )->pack(-side => 'left', -anchor => 'n',); # populate with some values unless done during initialisation $opt3->addOptions([M5=>5],[M6=>6],[M7=>7],[M8=>8],[M9=>9],[M10=>10], [ +NONE=>" "]); $tvar = $default_var; #set default $opt3-> configure( -textvariable => \$tvar ); ###################################################################### +############################################################ #Running the file my $run_lf = $mw->LabFrame( -label => 'Run', -labelside => 'acrosstop', -foreground => 'blue', ); $run_lf->pack( -expand => 1, -fill => 'both', ); my $but = $run_lf->Button( -text => "Go!", -command => \&push_button, )->pack; #Text Box my $txt = $run_lf->Scrolled( 'Text', -width => 40, -height => 15, -wrap => 'word', -scrollbars => 'e', -background => 'white', ); $txt->pack( -expand => 1, -fill => 'both', -padx => 5, -pady => 5, ); MainLoop; #SUB PROCEDURES sub push_button { $xcord2 = $x2->get(); $xcord3 = $x3->get(); $ycord2 = $y2->get(); $ycord3 = $y3->get(); $metalSelection2 = $opt2->get($var); $metalSelection3 = $opt3->get($var); $gdsname ||= '?'; $txt->insert( 'end', "Execution in progress\n" ); #$txt->update(); $txt->insert( 'end', "Operation is successful. Second co-ordinate deta +ils $xcord2 $ycord2 are co-ordinates. $metalSelection2 is selected. \ +n Third co-ordinate details $xcord3 $ycord3 are co-ordinates. $metalS +election3 is selected..\n" ); }

The text box should be able to print which are the co-ordinates and the metal options (M1, M2 e.t.c) selected by the user

please let me know how I should modify my code to achieve this

thanks,
  • Comment on Get value from Option menu in perl TK and set default value for option.
  • Download Code

Replies are listed 'Best First'.
Re: Get value from Option menu in perl TK and set default value for option.
by choroba (Cardinal) on Aug 03, 2016 at 09:23 UTC
    The answer to both the questions is the same: use the -(text)variables.

    As mentioned before, please try to remove unrelated parts of the code from your samples to make it more readable. Getting into practice of using strict and warnings would also save you a lot of trouble.

    #!/usr/bin/perl use warnings; use strict; use Tk; my %metal_name = map { $_ => "M$_" } 1 .. 4; my $default_metal = 3; my $default_x = 42; my $default_y = 3.14; my $mw = 'MainWindow'->new(-title => 'entries and option menu'); my $f_metal = $mw->Frame->pack; my $o_metal = $f_metal->Optionmenu( -options => [ map [ $metal_name{$_} => $_ ], sort keys %metal_name ], -variable => \ my $metal, -textvariable => \ my $t_metal, )->pack; # Set the default option. $metal = $default_metal; $t_metal = $metal_name{$default_metal}; my $f_coord = $mw->Frame->pack; $f_coord->Label(-text => 'X:')->pack(-side => 'left'); # Setting the default entry values. my $e_x = $f_coord->Entry(-textvariable => \(my $x = $default_x)) ->pack(-side => 'left'); my $e_y = $f_coord->Entry(-textvariable => \(my $y = $default_y)) ->pack(-side => 'right'); $f_coord->Label(-text => 'Y:')->pack(-side => 'right'); my $f_txt = $mw->Frame->pack; my $txt = $f_txt->Scrolled('Text')->pack; my $f_go = $mw->Frame->pack; $f_go->Button(-text => 'Go!', -command => sub { $txt->insert(end => join "\n", "Metal: $metal_name{$metal}", "Coords: [ $x, $y ]", q()); }, )->pack; MainLoop();

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found