Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Perl Tk & passing directories

by Anonymous Monk
on Aug 13, 2002 at 14:57 UTC ( #189811=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm a newbie in Perl Tk and am trying to write a program that lets a user to choose a directory, like c:\windows. My goal is so that an output file will be written to this directory after the program is finished.

The code (written by someone else) I'm using to access the file's directory is like this:

use Tk; use Tk::FileSelect; use Cwd; $top = new MainWindow; $fs = $top->FileSelect(-verify => [qw/-d/]); print $fs->Show; print "\n";

What I have so far is this:

When I run my program, two windows pop up. The first window (which does other stuff that my program needs) has an entry widget, and the second window is the one where I can select the directory (like the code above).

What I'm having trouble with:

When I select the directory, I want the directory to show up in the entry widget of my first window. i.e. I select "c:\windows" as my directory, so the words "c:\windows" should show up in my entry widget. It's kinda like how getOpenFile automatically returns the string for you.

The problem is that I don't know how to pass this string back to the first widow's entry widget. And will the computer recognize it as an actual directory, and not merely any old string? I just want the computer to eventually write an output file to this directory (i.e. the one that the user chooses). Am I going about this all wrong? Is there a better way?

Thanks for your help!

Replies are listed 'Best First'.
Re: Perl Tk & passing directories
by Shendal (Hermit) on Aug 13, 2002 at 15:58 UTC
    Maybe something like this will point you in the right direction?
    #!/usr/bin/perl -w use strict; use Tk; use Tk::FileSelect; my $top = new MainWindow; my $entry = $top->Entry()->pack(-side => 'left'); my $fs = $top->FileSelect(-verify => [qw/-d/]); my $dir = $fs->Show(); $entry->insert('end',$dir); MainLoop();

Re: Perl Tk & passing directories
by Rich36 (Chaplain) on Aug 13, 2002 at 16:00 UTC

    You will need to do two things. First, capture the return value of $fs->Show into a variable (i.e. my $dir = $fs->Show;. Then, use associate that variable with the entry widget. The Entry widget has a property called "-textvariable". By setting that to the variable that captures the user's input from the File Select (-textvariable => \$dir), the value of the Entry widget will reflect any changes in that variable.

      To Rich36 and Shendal,

      Thank you so very much for your help! Your suggestions are exactly what I've been looking for.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://189811]
Approved by ichimunki
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2021-12-03 00:48 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (27 votes). Check out past polls.