perl TK related (manupulating the output text area)

K

king

#!\c\perl\bin
use Tk;

my $mw = new MainWindow; # Main Window

my $frm_name = $mw -> Frame() -> pack();
my $lab = $frm_name -> Label(-text=>"Name:") -> pack();
my $ent = $frm_name -> Entry() -> pack();

my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button) ->
pack();
#Text Area
my $txt = $mw -> Text(-width=>50, -height=>30) -> pack();

MainLoop;

#This function will be executed when the button is pushed
sub push_button {
my $name = $ent -> get();
$txt -> insert('end',"Hello, $name.");
}

1.as soon as i run this script
a window opens
But the cursor is not on the name block
Question- Can it be possible that as soon as the window opens the
cursor should be present in the name block.

again
2. suppose once i typed a name,it will show up with hellow then the
$name.
but in the same window if I again give some other name in the name
block,
the hellow $ name is coming next to the previous test messege in the
text area.
Question
Each time I give a input and then enter button push me the only text
present there should be hellow and the recent name entered.
Can it be possible to do that.
 
P

Paul Lalli

king said:
#!\c\perl\bin
use Tk;

my $mw = new MainWindow; # Main Window

my $frm_name = $mw -> Frame() -> pack();
my $lab = $frm_name -> Label(-text=>"Name:") -> pack();
my $ent = $frm_name -> Entry() -> pack();

my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button) ->
pack();
#Text Area
my $txt = $mw -> Text(-width=>50, -height=>30) -> pack();

MainLoop;

#This function will be executed when the button is pushed
sub push_button {
my $name = $ent -> get();
$txt -> insert('end',"Hello, $name.");
}

1.as soon as i run this script
a window opens
But the cursor is not on the name block
Question- Can it be possible that as soon as the window opens the
cursor should be present in the name block.

Add the line:
$ent->focus();
right before MainLoop;
again
2. suppose once i typed a name,it will show up with hellow then the
$name.
but in the same window if I again give some other name in the name
block,
the hellow $ name is coming next to the previous test messege in the
text area.
Question
Each time I give a input and then enter button push me the only text
present there should be hellow and the recent name entered.
Can it be possible to do that.

Yes. Just delete the text that's currently in the box right before you
insert the new text. Look up the widget's delete() method at
http://search.cpan.org/~ni-s/Tk-804.027/pod/Text.pod#WIDGET_METHODS

Paul Lalli
 
B

Brian Raven

king said:
#!\c\perl\bin

Missing "use strict; use warnings;"
use Tk;

my $mw = new MainWindow; # Main Window

my $frm_name = $mw -> Frame() -> pack();
my $lab = $frm_name -> Label(-text=>"Name:") -> pack();
my $ent = $frm_name -> Entry() -> pack();

my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button) ->
pack();
#Text Area
my $txt = $mw -> Text(-width=>50, -height=>30) -> pack();

MainLoop;

#This function will be executed when the button is pushed
sub push_button {
my $name = $ent -> get();
$txt -> insert('end',"Hello, $name.");
}

1.as soon as i run this script
a window opens
But the cursor is not on the name block
Question- Can it be possible that as soon as the window opens the
cursor should be present in the name block.

See 'perldoc Tk::focus'
again
2. suppose once i typed a name,it will show up with hellow then the
$name.
but in the same window if I again give some other name in the name
block,
the hellow $ name is coming next to the previous test messege in the
text area.

Well, that is what you told it to do.
Question
Each time I give a input and then enter button push me the only text
present there should be hellow and the recent name entered.
Can it be possible to do that.

Then you have to delete what was already there before inserting a new
string. Look for the delete function in 'perldoc Tk::Text'.

HTH
 
Z

zentara

Script snipped. This should do what you want.

#!/usr/bin/perl
use warnings;
use strict;
use Tk;

my $mw = new MainWindow; # Main Window

my $frm_name = $mw -> Frame() -> pack();
my $lab = $frm_name -> Label(-text=>"Name:") -> pack();
my $ent = $frm_name -> Entry() -> pack();

my $but = $mw -> Button(-text=>"Push Me",
-command =>\&push_button) ->pack();

#Text Area
my $txt = $mw -> Text(-width=>50, -height=>30) -> pack();

$ent->focus;

MainLoop;

#This function will be executed when the button is pushed

sub push_button {
my $name = $ent -> get();
$txt -> insert('end',"Hello, $name.\n");
$ent->delete(0,'end');
}
__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top