Tk::getSaveFile - use specified initialfile

J

Josef Moellers

In an application which downloads an image from a webcam, I'd like to
save an image if it's interesting.
For that, I have a "Save image" button, which calls this sub:

sub save {
my $top = shift;
my @tv = localtime;

my $base = 'webcam-'
. sprintf('%04d.%02d.%02d', $tv[5]+1900, $tv[4]+1, $tv[3])
. '-'
. sprintf('%02d:%02d:%02d', $tv[2], $tv[1], $tv[0])
. '.pnm';

my $dst = $top->getSaveFile(-initialdir => $HOME,
-defaultextension => '.pnm',
-initialfile => $base,
-title => 'Save Snapshot',
-filetypes => [ ['Images', '.pnm' ],
['All files', '*' ] ]);

if (defined $dst) {
print STDERR "dst=$dst\n";
} else {
print STDERR "dst=<undef>\n";
}
if (defined $dst && $dst ne "") {
copy($tmpnam, $dst) or die "Copy failed: $!";
}
}

Unfortunately, I cannot just hit the "Save" button in the getSaveFile
window to use the given "webcam-..." name, I have to somehow make it
"dirty". How can I convince the getSaveFile widget to use this
initialfile without further action (i.e. just hit the "Save" button)?

Josef
 
B

Ben Morrow

Quoth Josef Moellers said:
In an application which downloads an image from a webcam, I'd like to
save an image if it's interesting.
For that, I have a "Save image" button, which calls this sub:

sub save {
my $top = shift;
my @tv = localtime;

my $base = 'webcam-'
. sprintf('%04d.%02d.%02d', $tv[5]+1900, $tv[4]+1, $tv[3])
. '-'
. sprintf('%02d:%02d:%02d', $tv[2], $tv[1], $tv[0])
. '.pnm';

I would replace this whole thing with POSIX::strftime:

use POSIX qw/strftime/;

my $base = strftime 'webcam-%Y.%m.%d-%H:%M:%S.pnm', localtime;
my $dst = $top->getSaveFile(-initialdir => $HOME,
-defaultextension => '.pnm',
-initialfile => $base,
-title => 'Save Snapshot',
-filetypes => [ ['Images', '.pnm' ],
['All files', '*' ] ]);

if (defined $dst) {
print STDERR "dst=$dst\n";
} else {
print STDERR "dst=<undef>\n";
}
if (defined $dst && $dst ne "") {
copy($tmpnam, $dst) or die "Copy failed: $!";
}
}

Unfortunately, I cannot just hit the "Save" button in the getSaveFile
window to use the given "webcam-..." name, I have to somehow make it
"dirty". How can I convince the getSaveFile widget to use this
initialfile without further action (i.e. just hit the "Save" button)?

The following script

#!/usr/bin/perl

use strict;
use warnings;

use Tk;
use POSIX qw/strftime/;

my $MW = Tk::MainWindow->new(-title => 'Test');
$MW->Button(
-command => sub {
my $base = strftime 'webcam-%Y.%m.%d-%H:%M:%S.pnm', localtime;

my $dst = $MW->getSaveFile(
-initialdir => $ENV{HOME},
-defaultextension => '.pnm',
-initialfile => $base,
-title => 'Save snapshot',
-filetypes => [
[ 'Images' => '.pnm' ],
[ 'All files' => '*' ],
],
);

$dst //= '<undef>'; # since you have dor :)
warn "dst=$dst";
},
-text => 'Save...',
)->pack;

Tk::MainLoop;

__END__

works for me with Tk v804.027; that is, I press 'Save...' and I get a
save-as box with the filename filled in, and I press 'Save' in the box
and perl prints

dst=/home/mauzo/webcam-2007.08.28-00:27:54.pnm at tksave line 26.

to stderr. What happens when you run this script?

Ben
 
J

Josef Moellers

Ben said:
Quoth Josef Moellers <[email protected]>:
my $dst = $top->getSaveFile(-initialdir => $HOME,
-defaultextension => '.pnm',
-initialfile => $base,
-title => 'Save Snapshot',
-filetypes => [ ['Images', '.pnm' ],
['All files', '*' ] ]);
my $dst = $MW->getSaveFile(
-initialdir => $ENV{HOME},
-defaultextension => '.pnm',
-initialfile => $base,
-title => 'Save snapshot',
-filetypes => [
[ 'Images' => '.pnm' ],
[ 'All files' => '*' ],
],
);
$dst //= '<undef>'; # since you have dor
to stderr. What happens when you run this script?

"Search pattern not terminated at Morrow line 25." ;-)

When I fix that, it does indeed return the filename.
What I don't get is: where's the difference between your call to
getSaveFile() and mine? I run both on the very same machine (SuSE Prof
9.0 with 800.024).

When I run my script on another machine with a more recent version of Tk
(Ubuntu 7.04 with 804.027), it behaves as expected. But again: on the
very same machine your script does the expected, mine doesn't.

BTW If I keep my version of constructing the filename, but add the "use
POSIX qw/strftime/;", it returns the filename. If I comment this out, it
doesn't.

Veeery strange to me.

Thanks,

Josef
 

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

No members online now.

Forum statistics

Threads
474,205
Messages
2,571,067
Members
47,673
Latest member
MahaliaPal

Latest Threads

Top