Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with capturing user input.

Status
Not open for further replies.

gammaman64

Programmer
Apr 28, 2007
9
US
Iam trying to write a function with a gui interface that allows the user to enter a file, and the system will check that file, and return the result. After they enter the file, how do I capture what they have entered, to be checked by the system.

sub check{

my $check = MainWindow->new(
-background=>'red',
-title=>'Type the file you wish to check');

my $entry = $check->Entry(
-textvariable=>\ my $txt);
my $entry_ok = $check->Button(
-text=>'OK'
-command=>);

$entry->pack();
$entry_ok->pack();


foreach my $file (@ARGV){

print ("Checking $file:");

if (-f $file){
print ("The file $file is:");
print ("readable")if(-r $file);
print ("writable")if(-w $file);
print ("executable") if (-x $file);


}

elsif (-d $file){
print ("$file is a directory");
while ($file)
{
my $count +=1;
}

 
First of all:

Code:
  my $entry = $check->Entry(
    -textvariable=>[COLOR=red]\ my $txt[/color]);

This isn't valid. You must declare $txt before using it in -textvariable.

Also, the code you have is instructed to begin its @ARGV loop regardless of what the user does. What you want to do is *pause* your code execution *until* the user has typed in a file, and then continue once the user has done so.

I'm not sure if you're aware of Tk::DialogBox, but this would be really helpful:

Code:
sub check {
   my $check = [COLOR=blue]$mw->DialogBox (
      -background => 'red',
      -title      => 'Type the file you wish to check',
      -buttons    => [ 'OK', 'Cancel' ],
      -default_button => 'OK',
   );[/color]

   my $txt;
   my $entry = $check->Entry (
      -textvariable => \$txt,
   )->pack;

   [COLOR=red]# We don't need to create a button;
    # Tk::DialogBox created some for us above.[/color]

   [COLOR=blue]# Show the DialogBox, and get the name of
   # the button that our user clicked. Only continue
   # if he clicked OK.
   my $clicked = $check->Show();

   # Don't continue if he clicked cancel.
   return unless $clicked eq 'OK';

   # Now, $txt is the value he entered into the text box.
   # Do with it what you want.
}[/color]

A DialogBox() is the same as a MainWindow, except for it comes with some buttons you can configure at launch time. You create a DialogBox, pack it with widgets just like you would a MainWindow, and then Show it.

The Show() method blocks until the DialogBox window is closed by the user. In this way, your code doesn't continue executing until the window is closed. So when you want to pause to get user input and then continue and do something with it, use a DialogBox. It's much more efficient than having to add a whole bunch of code to wait until they close the window.

Now, if you're just looking for a file name, look into $mw->getOpenFile, which actually opens a File Dialog Box from your operating system (which is, the standard dialog that all your applications share when you click "File->Open" from a menu bar).

(the screenshots here are on Linux; but trust me, on Windows, you get the native Windows dialog boxes)

-------------
Cuvou.com | The NEW Kirsle.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top