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

Re: Perk Tk - how to send a value from a subroutine back to the main program using Button (without MainLoop)

by Anonymous Monk
on Jun 07, 2013 at 02:25 UTC ( [id://1037550] : note . print w/replies, xml ) Need Help??


in reply to Perk Tk - how to send a value from a subroutine back to the main program using Button

You probably want to use Tk::Wizard, see http://search.cpan.org/dist/Tk-Wizard/MANIFEST, Confused by variable scope in Tk::Wizard (hello closures) , Re: Confused by variable scope in Tk::Wizard (hello closures)

But if you want to stick with MainLoop you could write that like this (all lexically scoped and not-memory leaking )

#!/usr/bin/perl -- # Test to enter select directory with gui # # # # # perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr - +ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use Encode; use Tk; use Path::Class; use Devel::CheckOS qw/ os_is /; Main( @ARGV ); exit( 0 ); sub Main { &wadebug; my $initialdir = shift || 'Z:\Projects\RunMSMS\MGF\human\ion_trap' +; my $end_file = shift || 'Z:\Projects\RunMSMS\MGF\human\ion_trap\ +test_gui_output.txt'; my $tissue = shift || "plasma"; tkgui( $initialdir, $end_file, $tissue ); } sub tkgui { &wadebug; my( $initialdir, $end_file, $tissue ) = @_; my $mw = Tk::MainWindow->new; my $tissue_but = $mw->Button( -text => 'Tissue Type', -command => [ \&select_tissue, $mw, $initialdir, \$tissue, ], ); $tissue_but->grid( -row => 3, -column => 1 ); $mw->Button( -text => 'Data Entry Complete', -command => [ \&do_end, $mw, $end_file, \$tissue, ], )->grid( -row => 10, -column => 1 ); Tk::MainLoop(); } sub select_tissue { &wadebug; my( $mw, $initialdir, $choiceref ) = @_; my $dir = $mw->chooseDirectory( -initialdir => $initialdir ); if( os_is(qw( MicrosoftWindows )) ){ $dir = encode( "windows-1252", $dir ); } if( defined $dir and length $dir ){ $dir = dir( $dir )->basename; if( length $dir ){ $$choiceref = $dir; } } return; } sub do_end { use autodie qw/ open close /; ## make open(...) or die(...) automa +tic &wadebug; my( $mw, $outfile, $tissueref ) = @_; print join " ", tissue => $$tissueref , "\n"; # open my($FOUT), '>>', $outfile; # print $FOUT "$$tissue\n"; # close $FOUT; $mw->destroy; ## exit mainloop } sub wadebug { my( $package, $filename, $line, $subroutine, $hasargs ) = caller( +1 ); for my $var ( @_ ){ if( not ref $var ){ use Data::Dump ();; print STDERR "# $line:$subroutine: @{[ Data::Dump::pp( $va +r ) ]}\n"; } elsif( ref($var) and ref($var) =~ m{ SCALAR | ARRAY | HASH } +x ) { print STDERR "# $line:$subroutine: @{[ Data::Dump::pp( $va +r ) ]}\n"; } else { print STDERR "# $line:$subroutine: $var\n"; } } print STDERR "\n"; } __END__ # 28:main::tkgui: "Z:\\Projects\\RunMSMS\\MGF\\human\\ion_trap" # 28:main::tkgui: "Z:\\Projects\\RunMSMS\\MGF\\human\\ion_trap\\test_g +ui_output.txt" # 28:main::tkgui: "plasma" # 251:main::select_tissue: Tk::MainWindow=HASH(0x9a1804) # 251:main::select_tissue: "Z:\\Projects\\RunMSMS\\MGF\\human\\ion_tra +p" # 251:main::select_tissue: \"plasma" # 251:main::do_end: Tk::MainWindow=HASH(0x9a1804) # 251:main::do_end: "Z:\\Projects\\RunMSMS\\MGF\\human\\ion_trap\\test +_gui_output.txt" # 251:main::do_end: \"x64" tissue x64
  • Comment on Re: Perk Tk - how to send a value from a subroutine back to the main program using Button (without MainLoop)
  • Download Code