perl TK related (manupulating the output text area)

Discussion in 'Perl Misc' started by king, Jun 2, 2006.

  1. king

    king Guest

    #!\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.
     
    king, Jun 2, 2006
    #1
    1. Advertising

  2. king

    Paul Lalli Guest

    king wrote:
    > #!\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
     
    Paul Lalli, Jun 2, 2006
    #2
    1. Advertising

  3. king

    Brian Raven Guest

    "king" <> writes:

    > #!\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

    --
    Brian Raven

    That which hits the fan tends to get flung in all directions.
    -- Larry Wall in <>
     
    Brian Raven, Jun 2, 2006
    #3
  4. king

    zentara Guest

    On 2 Jun 2006 01:53:40 -0700, "king" <> wrote:

    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__




    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html
     
    zentara, Jun 2, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    1
    Views:
    625
    Larry Barowski
    Jun 27, 2005
  2. KK
    Replies:
    2
    Views:
    340
    Jim Langston
    Jul 12, 2006
  3. Jan Müller

    manupulating a xml-file

    Jan Müller, Mar 31, 2008, in forum: Java
    Replies:
    3
    Views:
    370
    Jan Müller
    Mar 31, 2008
  4. leo
    Replies:
    1
    Views:
    306
    Bob Lehmann
    Dec 5, 2005
  5. king
    Replies:
    13
    Views:
    162
    Charles DeRykus
    Jun 8, 2006
Loading...

Share This Page