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 }

In reply to Perk Tk - how to send a value from a subroutine back to the main program using Button by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.