原文:
11.5. How do I redraw a line on a Canvas?
By calling the ->coord method on the item as in the following example: #!/usr/bin/perl use Tk; $m = MainWindow->new; $c = $m -> Canvas; $i = $c -> create('line', 0,0 => 50,50 ); $c -> pack; $b = $m -> Button('-text' => 'extend', '-command' => sub{push_it($c,$i)}, )->pack; MainLoop; sub push_it { my ($canvas, $line) = @_; $canvas -> coords($line, 0,0 => 100,100 ); }
Thanks to Christopher Dunn and Harry Bochner <bochner@das.harvard.edu> for providing this question and answer.
译文:
11.5. 如何调整画布中线条的位置?
这可以通过对这个元件(line)调用->coord方法来实现,例如:
#!/usr/bin/perl
use Tk;
$m = MainWindow->new;
$c = $m -> Canvas;
$i = $c -> create('line', 0,0 => 50,50 );
$c -> pack;
$m -> Button('-text' => 'extend',
'-command' => sub{push_it($c,$i)},
)->pack;
MainLoop;
sub push_it {
my ($canvas, $line) = @_;
$canvas -> coords($line, 0,0 => 100,100 );
}
感谢Christopher Dunn和Harry Bochner提供了这个问题及答案!
(译者注:其实对于线条的调整不仅限于位置,其它的各种属性都是可以的——使用itemconfigure方法来实现!有兴趣的朋友可以试试在上面的例子中的子程序的最后加上这样一行来看看效果如何:$canvas->itemconfigure($line,-width=>3,-fill= >'red',-arrow=>'both');。另外,这个方法可是对画布中各种元件都有用的哟!)