Entry Widget - Perl/Tk

Discussion in 'Perl Misc' started by doni, Mar 8, 2007.

  1. doni

    doni Guest

    Hi,

    I am trying to do some error checking on my Entry Widget in this Perl/
    Tk test program. I would like to check if the user has entered some
    text under Filename before he starts pressing the "Load" button. Will
    "validate command" option under Entry Widget work for this. Can anyone
    point me how should I go about this.

    Here is the source code.
    #!/usr/bin/perl

    use Tk;

    $mw = MainWindow->new;
    $mw->configure(-title=>'Test',
    -background=>'blue',
    -width=>"700",
    -height=>"500");

    $info = "Start Loading Filename...";

    # Create necessary widgets
    $f = $mw->Frame->pack(-side => 'top',
    -fill => 'x');
    $f->Label(-text => "Filename:")->pack(-side => 'left',
    -anchor =>
    'center');
    $f->Entry(-textvariable => \$filename)->pack(-side => 'left',
    -
    anchor => 'w',
    -fill
    => 'x',
    -
    expand => 1);
    $f->Button(-text => "Exit",
    -activebackground => "red",
    -underline => 1,
    -command => sub {exit;})->pack(-side => 'right');
    $f->Button(-text=>"Clear",
    -command=> sub {$t->delete("0.0",'end')})->pack(-
    side=>'right',

    -anchor=>'s');
    $f->Button(-text => "Save",
    -command => \&save_file)->pack(-side => 'right',
    -
    anchor => 'e');
    $f->Button(-text => "Load",
    -command => \&load_file)->pack(-side => 'right',
    -
    anchor => 'e');
    $mw->Label(-textvariable => \$info,
    -relief => 'ridge')->pack(-side => 'bottom',
    -fill => 'x');
    $t = $mw->Scrolled("Text")->pack(-side => 'bottom',
    -fill => 'both',
    -expand => 1);
    MainLoop;

    # load_file checks to see what the filename is and loads it if
    possible
    sub load_file {
    $info = "Loading file '$filename'...";
    $t->delete("1.0", "end");
    if (!open(FH, "/$filename")) {
    $t->insert("end", "ERROR: Could not open $filename\n");
    return;
    }
    while (<FH>) { $t->insert("end", $_); }
    close (FH);
    $info = "File '$filename' loaded";
    }

    # save_file saves the file using the filename in the Entry box.
    sub save_file {
    $info = "Saving '$filename'";
    open (FH, ">$filename");
    print FH $t->get("1.0", "end");
    $info = "Saved.";
    }

    Thanks,
    doni
     
    doni, Mar 8, 2007
    #1
    1. Advertising

  2. I do not know if validate would work. My solution was to have the
    button disabled until the "<<Modified>>" virtual event, then change
    its state to active.

    Hope that helps. There might be a better way. I am at best an
    experienced novice.

    On Mar 8, 1:41 pm, "doni" <> wrote:
    > Hi,
    >
    > I am trying to do some error checking on my Entry Widget in this Perl/
    > Tk test program. I would like to check if the user has entered some
    > text under Filename before he starts pressing the "Load" button. Will
    > "validate command" option under Entry Widget work for this. Can anyone
    > point me how should I go about this.
    >
    > Here is the source code.
    > #!/usr/bin/perl
    >
    > use Tk;
    >
    > $mw = MainWindow->new;
    > $mw->configure(-title=>'Test',
    > -background=>'blue',
    > -width=>"700",
    > -height=>"500");
    >
    > $info = "Start Loading Filename...";
    >
    > # Create necessary widgets
    > $f = $mw->Frame->pack(-side => 'top',
    > -fill => 'x');
    > $f->Label(-text => "Filename:")->pack(-side => 'left',
    > -anchor =>
    > 'center');
    > $f->Entry(-textvariable => \$filename)->pack(-side => 'left',
    > -
    > anchor => 'w',
    > -fill
    > => 'x',
    > -
    > expand => 1);
    > $f->Button(-text => "Exit",
    > -activebackground => "red",
    > -underline => 1,
    > -command => sub {exit;})->pack(-side => 'right');
    > $f->Button(-text=>"Clear",
    > -command=> sub {$t->delete("0.0",'end')})->pack(-
    > side=>'right',
    >
    > -anchor=>'s');
    > $f->Button(-text => "Save",
    > -command => \&save_file)->pack(-side => 'right',
    > -
    > anchor => 'e');
    > $f->Button(-text => "Load",
    > -command => \&load_file)->pack(-side => 'right',
    > -
    > anchor => 'e');
    > $mw->Label(-textvariable => \$info,
    > -relief => 'ridge')->pack(-side => 'bottom',
    > -fill => 'x');
    > $t = $mw->Scrolled("Text")->pack(-side => 'bottom',
    > -fill => 'both',
    > -expand => 1);
    > MainLoop;
    >
    > # load_file checks to see what the filename is and loads it if
    > possible
    > sub load_file {
    > $info = "Loading file '$filename'...";
    > $t->delete("1.0", "end");
    > if (!open(FH, "/$filename")) {
    > $t->insert("end", "ERROR: Could not open $filename\n");
    > return;
    > }
    > while (<FH>) { $t->insert("end", $_); }
    > close (FH);
    > $info = "File '$filename' loaded";
    >
    > }
    >
    > # save_file saves the file using the filename in the Entry box.
    > sub save_file {
    > $info = "Saving '$filename'";
    > open (FH, ">$filename");
    > print FH $t->get("1.0", "end");
    > $info = "Saved.";
    >
    > }
    >
    > Thanks,
    > doni
     
    Matthew Mundy, Mar 8, 2007
    #2
    1. Advertising

  3. doni

    doni Guest

    On Mar 8, 12:55 pm, Michele Dondi <> wrote:
    > Well, here there may well be people able to help you on Tk, but as I
    > wrote in my other post to you (well, maybe you didn't receive it) you
    > may want to check comp.lang.perl.tk!
    >
    > Michele
    > --


    thanks, I got the answer to my solution from comp.lang.perl.tk.
    I posted the same question earlier in comp.lang.perl.tk as well. I
    dont know if I am not supposed to do like that. Let me know if I am
    not supposed to post the same question in 2 groups.

    Thanks,
    doni
     
    doni, Mar 8, 2007
    #3
  4. doni

    doni Guest

    On Mar 8, 3:01 pm, Michele Dondi <> wrote:
    > You're supposed not to. If there's a *really* good reson to do so,
    > then you'd better crosspost rather than multipost, i.e. include both
    > groups in the Newsgroups header. So people from both will know the
    > answers you received and this will avoid duplicate efforts. If you
    > find that many people sets Followup-to one group only, then you
    > probably shouldn't have crossposted in the first place.
    >
    > Michele


    thanks for letting me know.

    doni
     
    doni, Mar 8, 2007
    #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. Phil Schmidt
    Replies:
    3
    Views:
    585
    Michael Peuser
    Sep 6, 2003
  2. Replies:
    0
    Views:
    347
  3. Frederic Rentsch
    Replies:
    0
    Views:
    266
    Frederic Rentsch
    Jul 8, 2012
  4. Terry Reedy
    Replies:
    15
    Views:
    662
    Frederic Rentsch
    Jul 16, 2012
  5. Frederic Rentsch
    Replies:
    0
    Views:
    453
    Frederic Rentsch
    Jul 9, 2012
Loading...

Share This Page