FILE SELECTION DIALOG PROBLEM

G

Go Perl

I am using the following code to create file selection dialogs..But
apparently the Browse buttons do not seem to work.. i got this from
examples provided by typing widget. I changed a little to remove the
radio buttons and stuff, but i am not able to browse and select the
files. I will be glad if anyone can point the error and throw some
light on this.
Thanks very much.

use Tk;
$mw = MainWindow->new();

foreach my $i (qw(one two three four)) {
my $f = $mw->Frame;
my $lab = $f->Label(-text => "Select $i file to Open: ",
-anchor => 'e');
my $ent = $f->Entry(-width => 20);
my $but = $f->Button(-text => "Browse ...",
-command => sub { fileDialog($mw, $ent, $i)});
$lab->pack(-side => 'left');
$ent->pack(-side => 'left',-expand => 'yes', -fill => 'x');
$but->pack(-side => 'left');
$f->pack(-fill => 'x', -padx => '1c', -pady => 3);
}


sub filebox {
my $demo = shift;


(
-name => $demo,
-text => "Enter a file name in the entry box or click on
the \"Browse\" buttons to select a file name

using the file selection dialog.",
-iconname => 'filebox',
);
}

sub fileDialog {
my $w = shift;
my $ent = shift;
my $operation = shift;
my $types;
my $file;
@types =
(["Text files", '*.txt'],
["All files", '*']
);
if ($operation eq 'open') {
$file = $w->getOpenFile(-filetypes => \@types);
}

}
 
B

Bob Walton

Go said:
I am using the following code to create file selection dialogs..But
apparently the Browse buttons do not seem to work.. i got this from
examples provided by typing widget. I changed a little to remove the
radio buttons and stuff, but i am not able to browse and select the
files. I will be glad if anyone can point the error and throw some
light on this. ....

use Tk;
$mw = MainWindow->new();

foreach my $i (qw(one two three four)) {
my $f = $mw->Frame;
my $lab = $f->Label(-text => "Select $i file to Open: ",
-anchor => 'e');
my $ent = $f->Entry(-width => 20);
my $but = $f->Button(-text => "Browse ...",
-command => sub { fileDialog($mw, $ent, $i)});
$lab->pack(-side => 'left');
$ent->pack(-side => 'left',-expand => 'yes', -fill => 'x');
$but->pack(-side => 'left');
$f->pack(-fill => 'x', -padx => '1c', -pady => 3);
}


MainLoop; #is needed here

sub filebox {
my $demo = shift;


(
-name => $demo,
-text => "Enter a file name in the entry box or click on
the \"Browse\" buttons to select a file name

using the file selection dialog.",
-iconname => 'filebox',
);
}

sub fileDialog {
my $w = shift;
my $ent = shift;
my $operation = shift;
my $types;
my $file;
@types =
(["Text files", '*.txt'],
["All files", '*']
);
if ($operation eq 'open') {
$file = $w->getOpenFile(-filetypes => \@types);
}

}

Your "browse" buttons work fine. The sub you are calling with the
-command of each Button is not given a third argument of 'open',
however, so it appears as if the Button does nothing. If you change it
so the the third argument is 'open' instead of $i, then the file dialog
does appear. But then, you don't do anything with $file once you have
it, so your program needs lots more work.

And BTW, your example is missing a MainLoop; statement in the main program.
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top