Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I can create buttons in TK and a text field but how do you store the input into a variable after they click the button? Can someone come up with a really quick example?

Thanks.

Replies are listed 'Best First'.
Re: TK form submission
by sulfericacid (Deacon) on Jun 06, 2004 at 06:05 UTC
    Taken from "widgets" from the command line.
    my $file_name = ''; my $file = $TOP->Frame; my $fn = $file->LabEntry(-label => 'File Name: ', -width => 4 +0, -labelPack => [qw/-side left -anchor w/], -textvariable => \$file_name)->pack(qw/-side left/)


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
Re: TK form submission
by mawe (Hermit) on Jun 06, 2004 at 09:18 UTC
    use Tk; my $top = new MainWindow; my $but = $top->Button(text=>"OK", command=>\&get_text)->pack; my $tex = $top->Text()->pack; MainLoop; sub get_text { $text = $tex->get(0.1,'end'); print $text; }
      Can you explain what $text = $tex->get(0.1,'end'); is? I mean, what is get(0.1,'end') ?

      Thanks!

        It should be "1.0" instead of "0.1" and it means: get the whole contents of the text widget, that is from line 1 column 0 to the end.
        characters in a Text widget are referenced by text indexes. The parameters of the get method are the start index and stop index (which is optional). Indexes are denoted in line.character format. Line numbers in a Text widget start at 1, and characters on a line start at 0. end is a special index referring to the end of the widget or line. So, for example:

        my $text = $widget->get("2.0", "5.end")

        means get everything from line 2, character 1 to the end of line 5 and place the content into the variable $text.

        For a more detailed explanation, refer to O'reilly's "Mastering Perl/Tk." If you are writing gui's in Perl/Tk it is a most valuable resource.

        hope this helps,
        davidj