如果你关闭你的Perl脚本,它结束。它不记得任何东西,它不再存在。 但是,您可以使脚本将位置保存到文件中,并且在启动时,您可以尝试从文件中读取位置。请看下面的例子:
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $file = 'input.txt';
my $config = '.viewtext.conf';
my $mw = MainWindow->new(-title => 'Remember Position');
my $t = $mw->Scrolled("Text", -scrollbars => 'se')->pack;
my $b = $mw->Button(-text => 'Quit', -command => \&quit)->pack;
open my $FH, '<', $file or die $!;
$t->Contents(<$FH>);
close $FH;
if (-f $config) {
open my $FH, '<', $config or die $!;
my ($x0, $x1, $y0, $y1) = split//, <$FH>;
$t->xviewMoveto($x0); # Here we restore the saved position
$t->yviewMoveto($y0);
}
MainLoop();
sub quit {
open my $CONF, '>', $config or die $!;
print {$CONF} join ' ', $t->xview, $t->yview; # This is the current position
close $CONF;
$mw->destroy;
}
如果你要打开几个不同的文件,你必须与位置一起记住文件的路径。
非常感谢!!真的解决了它! – dileepmani