Beginner Question about Text Box 
Author Message
 Beginner Question about Text Box

Hi, I am new to Perl and Tk and am having trouble finding tutorials
and/or reference websites on the internet ( at least ones that don't
charge a fee).  So if anyone knows of any good links to perl/tk
tutorials I can learn more on my own.

I am creating a GUI for an existing perl program and am having trouble
getting the text from a text box when running the subroutine called
from a button.  I tried to make a simple example program to test how
to do it but I can't get it to work correctly.  The compiler says that
$text_1 (the name of the text box) is not defined in the sub.  I just
need to know the basics of how to get data from a text box, process
it, and display it to the screen (in a label or read only text area).
I used a program called specPerl to create the interface and then
added the code for the interactive part in a text editor.

 # Sample SpecTcl main program for testing GUI

use Tk;
my($top) = MainWindow->new();
$top->title("untitled test");
my($main_frame) = $top->Frame()->pack(
        -fill=>'both',
        -expand=>1
);

# interface generated by SpecTcl (Perl enabled) version 1.1
# from untitled.ui
# For use with Tk400.202, using the gridbag geometry manager

sub run {
        my $word;
        $word = $text_1->get(0,6);
        $label_1->configure( -text => $word );

Quote:
}

sub untitled_ui {

        # widget creation

        my($label_1) = $root->Label (
                -text => 'label',
        );
        my($text_1) = $root->Text (
                -height => '1',
                -width => '1',
        );
        my($button_1) = $root->Button (
                -text => 'button',
        );

        # Geometry management

        $label_1->grid(
                -in => $root,
                -column => '1',
                -row => '1'
        );
        $text_1->grid(
                -in => $root,
                -column => '1',
                -row => '2',
                -sticky => 'nesw'
        );
        $button_1->grid(
                -in => $root,
                -column => '2',
                -row => '2',
        );

        # Resize behavior management

        # container $root (rows)
        $root->gridRowconfigure(1, -weight  => 0, -minsize  => 30);
        $root->gridRowconfigure(2, -weight  => 0, -minsize  => 30);

        # container $root (columns)
        $root->gridColumnconfigure(1, -weight => 0, -minsize => 30);
        $root->gridColumnconfigure(2, -weight => 0, -minsize => 30);

        # additional interface code
        $button_1->configure(
                -command=> \&run
        );

        # end additional interface code

Quote:
}

untitled_ui $main_frame;
Tk::MainLoop;

1;

Any help or references to tutorials or maybe another editor like
specPerl that is more up to date would be much appreciated.  Thanks.
-Nick



Tue, 29 Mar 2005 04:31:47 GMT  
 Beginner Question about Text Box

Quote:
> Hi, I am new to Perl and Tk and am having trouble finding tutorials
> and/or reference websites on the internet ( at least ones that don't
> charge a fee).  So if anyone knows of any good links to perl/tk
> tutorials I can learn more on my own.

The 'widget' demo which comes with the source, is great to learn from.

Quote:

> I am creating a GUI for an existing perl program and am having trouble
> getting the text from a text box when running the subroutine called
> from a button.  I tried to make a simple example program to test how
> to do it but I can't get it to work correctly.  The compiler says that
> $text_1 (the name of the text box) is not defined in the sub

[lots of snipping]

Quote:
> sub run {
> my $word;
> $word = $text_1->get(0,6);
> $label_1->configure( -text => $word );
> }

> sub untitled_ui {
> my($text_1) = $root->Text (
  ^^^^^^^^^^^
> -height => '1',
> -width => '1',
> );
> }

Throw a 'use strict' at the top of your program, and you will see the problem.
Your $text_1 is lexical to the subroutine which created it, so your 'run'
subroutine doesn't even know what $text_1 is.

Here is an example of what you are trying to do (I think):
#####################
use Tk;
use strict;

my $var; #Now accessible by all subs !

my $mw=new MainWindow;
my $label=$mw->Label(-textvariable=>\$var)->grid(
 -row=>0,
 -column=>0,
 -columnspan=>2);

#Are you sure you don't want to use an 'Entry' ??
my $text=$mw->Text(-height=>1, -width=>20)->grid(
 -row=>1,
 -column=>0);
my $button=$mw->Button(-text=>'Run',-command=>\&run)->grid(
 -row=>1,
 -column=>1
 );

MainLoop;

sub run
{
 $var=$text->get('1.0','end');
 $var=~s/\n$//; #this wouldn't be needed with an Entry.

Quote:
}

#####################

Jack



Tue, 29 Mar 2005 06:29:50 GMT  
 Beginner Question about Text Box
Thank you very much that works out now.  

I found a FAQ for Perl/Tk at www.faq.org that offers some help in
other ways (like passing the text boxs as parameters to the button
subroutines) but it looks like it hadn't been updated for a number of
years.

I had some other questions though about using specPerl.  
Does anyone else use this program to make the GUI?  I am having
trouble finding a way to reference a widget through it's parent
window.  I want to do this because the code generated by specPerl puts
all the GUI initialization stuff in one subroutine so I want to refer
to those widgets without making all the widgets global (which isn't
too much trouble I suppose).  The code generated by specPerl does make
the variable $main_frame global and passes it to the GUI
initialization subroutine.  I know that I could do this easily enough
in Java/Swing with something like mainwindow.textbox1.gettext();
but when I try in Perl/Tk I don't know the syntax I tried
$main_frame->$text_1->get('1.0','end') but it isn't the right syntax.
The examples that come with specPerl don't actually have any
functional code behind the GUI so I didn't see how to have the main
Perl program communicate with the widgets in the GUI.

Also I am wondering about the Entry widget.  I didn't use this for my
program because I though there might be some size limitation, so I
used a textbox (the input from the user will be very large, 300+
characters).  But I couldn't find any reference to any limits on the
text size for the entry vs textbox.  The entry widget lets you set a
property called textvariable which I think lets you avoid having to
call the get method so that might be easier to use.

Thanks.
-Nick

Quote:
>....
> Throw a 'use strict' at the top of your program, and you will see the problem.
> Your $text_1 is lexical to the subroutine which created it, so your 'run'
> subroutine doesn't even know what $text_1 is.
>....
> Jack



Wed, 30 Mar 2005 03:33:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Beginner's question on clearing Entry/Text boxes

2. List Boxes & Text Boxes

3. Another Text Box question

4. Another Text box question

5. BEGINNER'S question re searching for text

6. substituting text in a var (beginner question)

7. Text formatting using Text Boxes and CGI

8. Win32 Text Box Problems in Windows 98, 98se and ME

9. apostrophe character in text boxes and mysql

10. Limiting length of datasent from a text box

11. Adding to and deleting from list boxes, text areas

12. Text Submission in Data Capture boxes

 

 
Powered by phpBB® Forum Software