Tk::getSaveFile - use specified initialfile
Tk::getSaveFile - use specified initialfile
am 27.08.2007 16:58:28 von Josef Moellers
In an application which downloads an image from a webcam, I'd like to=20
save an image if it's interesting.
For that, I have a "Save image" button, which calls this sub:
sub save {
my $top =3D shift;
my @tv =3D localtime;
my $base =3D '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 =3D $top->getSaveFile(-initialdir =3D> $HOME,
-defaultextension =3D> '.pnm',
-initialfile =3D> $base,
-title =3D> 'Save Snapshot',
-filetypes =3D> [ ['Images', '.pnm' ],
['All files', '*' ] ]);
if (defined $dst) {
print STDERR "dst=3D$dst\n";
} else {
print STDERR "dst=3D\n";
}
if (defined $dst && $dst ne "") {
copy($tmpnam, $dst) or die "Copy failed: $!";
}
}
Unfortunately, I cannot just hit the "Save" button in the getSaveFile=20
window to use the given "webcam-..." name, I have to somehow make it=20
"dirty". How can I convince the getSaveFile widget to use this=20
initialfile without further action (i.e. just hit the "Save" button)?
Josef
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
Re: Tk::getSaveFile - use specified initialfile
am 28.08.2007 01:29:48 von Ben Morrow
Quoth 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';
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=\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 //= ''; # 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
--
"Awww, I'm going to miss her."
"Don't you hate her?"
"Yes, with a fiery vengeance."
[ben@morrow.me.uk]
Re: Tk::getSaveFile - use specified initialfile
am 28.08.2007 09:16:20 von Josef Moellers
Ben Morrow wrote:
> Quoth Josef Moellers :
>> my $dst =3D $top->getSaveFile(-initialdir =3D> $HOME,
>> -defaultextension =3D> '.pnm',
>> -initialfile =3D> $base,
>> -title =3D> 'Save Snapshot',
>> -filetypes =3D> [ ['Images', '.pnm' ],=
>> ['All files', '*' ] ])=
;
> my $dst =3D $MW->getSaveFile(
> -initialdir =3D> $ENV{HOME},
> -defaultextension =3D> '.pnm',
> -initialfile =3D> $base,
> -title =3D> 'Save snapshot',
> -filetypes =3D> [
> [ 'Images' =3D> '.pnm' ],
> [ 'All files' =3D> '*' ],
> ],
> );
> $dst //=3D ''; # since you have dor
> to stderr. What happens when you run this script?=20
"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=20
getSaveFile() and mine? I run both on the very same machine (SuSE Prof=20
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=20
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=20
POSIX qw/strftime/;", it returns the filename. If I comment this out, it =
doesn't.
Veeery strange to me.
Thanks,
Josef
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html