Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have been using Perl for a while, but have recently started using the Perl Tk module. I am trying to make a GUI that will ask for a bunch of information and then output it to a file, among other things. (Actually, I am trying to update a Perl program that does this via text inquiries in a command line.) I have successfully added Radiobuttons, text Entry and a regular Button to close the window and print out values at the end. I am stuck on trying to use the Button to call a subroutine that will send a value back to the main program.
I want to use a button to call a subroutine that will use ChooseDirectory to select a subdirectory and send it back to the main program. I tried several variations on the -command value, but none of them worked. If I put ChooseDirectory in the main window, instead of calling it from a button, it pops up before the general window asking for other information. It does send the correct information though.
I am copying some code here. This is for a smaller program that only tries to ask for the directory. Thanks for any help.
# Test to enter select directory with gui # Good to use next 3 lines for all perl programs. #! /usr/bin/perl use warnings; use strict; # Use Tk module for gui and create window use Tk; my $mw = new MainWindow; use Encode; #so can read directories #Button to open directory and see tissue types my $tissue="plasma";#default value my $tissue_but = $mw -> Button(-text => 'Tissue Type', -command =>sub +{select_tissue()}); $tissue_but->grid(-row=>3,-column=>1); #-command => [ sub { ... }, arg1, arg2, ... ] #Button to close gui window my $but=$mw -> Button(-text => 'Data Entry Complete', -command =>sub { +do_end()}); $but->grid(-row=>10,-column=>1); #This must be at the end of every Tk section MainLoop; sub select_tissue { my $dir=$mw-> chooseDirectory(-initialdir =>'Z:\Projects\RunMSMS\M +GF\human\ion_trap'); $dir = encode("windows-1252", $dir); my @fulldir=split(/\//,$dir);#split out tissue type from full dire +ctory my $tis=$fulldir[6]; return $tis; } sub do_end # { my ($tissue)=@_; open INFO_OUT, ">>test_gui_output.txt" or die $!; print INFO_OUT "$tissue\n"; exit; #close gui window }
|
---|