Home > Archive > PerlTk > March 2005 > Removing or Unmapping objects on a canvas..
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Removing or Unmapping objects on a canvas..
|
|
| Nash Aragam 2005-03-08, 3:59 pm |
| Hi,
Does anybody know how to erase, unmap, delete a large number of objects
(say, a collection of spectral lines) and widgets on a canvas w/o
removing the canvas itself ? I don't want to delete each individual
object one at a time. I just want to go back to a clean slate after
using the canvas for diplaying a plot and labels associated with the
plot. Any help is appreciated...TiA,
Nash
| |
| thundergnat 2005-03-08, 3:59 pm |
| Nash Aragam wrote:
> Hi,
>
> Does anybody know how to erase, unmap, delete a large number of objects
> (say, a collection of spectral lines) and widgets on a canvas w/o
> removing the canvas itself ? I don't want to delete each individual
> object one at a time. I just want to go back to a clean slate after
> using the canvas for diplaying a plot and labels associated with the
> plot. Any help is appreciated...TiA,
>
> Nash
>
Tk::Canvas doesn't seem to have a "bulk delete" function, so you
are probably going to have to delete them one at a time.
You can save yourself some aggravation by saving the object referances
in an array when you create them. Then you can just cycle through the
array to delete them then clear the array.
Umm. Let's see... Here's a script that demonstrates that.
This is something I was just messing around with.
########################################
####################3
#!/user/bin/perl
use strict;
use warnings;
use Tk;
my $mw = MainWindow->new();
my $current;
my @currentxy;
my @objects;
my @colors = qw(red green blue orange yellow
purple tan black white pink);
my $can = $mw->Canvas(
-height => 300,
-width => 250,
)->pack(
-fill => 'both',
-expand => 'y'
);
$mw->Button(
-text => 'Fill',
-width => 10,
-command => sub{del(); fill();}
)->pack;
$mw->Button(
-text => 'Empty',
-width => 10,
-command => \&del
)->pack;
$mw->update;
fill();
choose($current, $objects[0]);
move($current,0,0);
$can->CanvasFocus;
MainLoop;
sub fill{
for (0..50){
my $x = rand($can->width);
my $y = rand($can->height);
my $color = $colors[rand(@colors)];
$objects[$_] = $can->createOval( $x, $y,
($x+25), ($y+25), -fill => $color);
}
for (@objects){
bindchoose($_);
bindmouse($_);
}
}
sub del{
$can->delete("$_") for @objects;
@objects = ();
}
sub move{
$can->move(@_);
$currentxy[0] += $_[1];
$currentxy[1] += $_[2];
}
sub choose{
$can->itemconfigure($_[0], -outline => 'black', -width => 1.0);
$can->itemconfigure($_[1], -outline => 'purple', -width => 3.0);
$currentxy[0] = $can->canvasx($mw->pointerx);
$currentxy[1] = $can->canvasy($mw->pointery);
$current = $_[1];
$can->raise($current);
}
sub mousemove{
my $x = $can->canvasx($mw->pointerx) - $currentxy[0];
my $y = $can->canvasy($mw->pointery) - $currentxy[1];
move($current, $x, $y);
}
sub bindchoose{
my $object = shift;
$can->bind($object, '<1>' => sub{
choose($current, $object);
$current = $object;
move($current, 0, 0);
}
);
}
sub bindmouse{
my $object = shift;
$can->bind($object, '<B1-Motion>' => \&mousemove);
}
| |
| Steve Lidie 2005-03-09, 3:58 am |
| Nash Aragam <naragam@acm.org> wrote:
> Hi,
>
> Does anybody know how to erase, unmap, delete a large number of objects
> (say, a collection of spectral lines) and widgets on a canvas w/o
> removing the canvas itself ? I don't want to delete each individual
> object one at a time. I just want to go back to a clean slate after
> using the canvas for diplaying a plot and labels associated with the
> plot. Any help is appreciated...TiA,
>
> Nash
>
When you create the Canvas items, assign them all a special tag, then
delete by tag name:
$canvas->createXXX( .... -tags => [ 'tag1', 'tag2', ... 'delete-me' ] );
..
..
..
$canvas->delete( 'delete-me' );
| |
|
| On Tue, 08 Mar 2005 13:30:28 -0500, thundergnat wrote:
> Nash Aragam wrote:
> Tk::Canvas doesn't seem to have a "bulk delete" function, so you are
> probably going to have to delete them one at a time.
The docs say "The tag all is implicitly associated with
every item in the canvas", so if Nash's "a large number
of objects" == everything, then $canvas->delete('all');
should do it.
plugh
|
|
|
|
|