tk canvas question

J

Joe Van Dyk

Hi,

I'm drawing stuff on a TkCanvas. Essentially, a bunch of shapes that
move around. I need to keep track of where each shape has been, so I
can draw the shape's history if needed (a "slug track").

So every time a shape move, I store its position. When I want to view
a shape's history, I draw a line connecting each stored position.

I'm currently drawing all of these lines directly on to the main canvas. =
=20

The problem is when I want to hide the history lines. There can be
400 shapes, and each of them could have thousands of lines that detail
their previous history. Going through and deleting all the TkcLines
is taking a very long time.

Is there a way to "group" each shape's history lines and then be able
to give one command that shows and hides the lines in that group?

Thanks,
Joe
 
J

Joe Van Dyk

Hi,
=20
I'm drawing stuff on a TkCanvas. Essentially, a bunch of shapes that
move around. I need to keep track of where each shape has been, so I
can draw the shape's history if needed (a "slug track").
=20
So every time a shape move, I store its position. When I want to view
a shape's history, I draw a line connecting each stored position.
=20
I'm currently drawing all of these lines directly on to the main canvas.
=20
The problem is when I want to hide the history lines. There can be
400 shapes, and each of them could have thousands of lines that detail
their previous history. Going through and deleting all the TkcLines
is taking a very long time.
=20
Is there a way to "group" each shape's history lines and then be able
to give one command that shows and hides the lines in that group?
=20
Thanks,
Joe


Aha. I found something called TkcGroup. I'm having difficulties
figuring out how to remove and/or hide the canvas objects that are in
each group though.
 
J

Joe Van Dyk

=20
I found this code out there somewhere:
=20
#!/usr/bin/env ruby
=20
require "tk"
=20
canvas =3D TkCanvas.new
grp =3D TkcGroup.new(canvas)
grp.add(TkcRectangle.new(canvas, '1c', '2c', '3c', '3c',
'outline' =3D> 'black', 'fill' =3D> 'blu= e'))
canvas.pack
=20
canvas.bind 'Enter' do
grp.configure 'fill', 'red'
end
canvas.bind 'Leave' do
grp.configure 'fill', 'green'
end
=20
Tk.mainloop
=20
=20
I think it's supposed to have the rectangle change colors when the
cursor enters and leaves the canvas. But I don't see any color change
occuring. Can someone confirm this for me please?
=20

Here's the link to the original tk code, and the corresponding Ruby code.

http://www2s.biglobe.ne.jp/~Nori/ruby/ja/tk-ref/itemconfigure.html

The Tk code works for me, while the Ruby one doesn't.
 
H

Hidetoshi NAGAI

From: Joe Van Dyk <[email protected]>
Subject: Re: tk canvas question
Date: Wed, 22 Jun 2005 06:15:41 +0900
Message-ID: said:
I found this code out there somewhere: (snip)
grp.add(TkcRectangle.new(canvas, '1c', '2c', '3c', '3c',
'outline' => 'black', 'fill' => 'blue'))

Please use TkcGroup#include.
For example,
-----------------------------------------------
require "tk"

canvas = TkCanvas.new
grp = TkcGroup.new(canvas)
grp.include(TkcRectangle.new(canvas, '1c', '2c', '3c', '3c',
'outline' => 'black', 'fill' => 'blue'),
TkcRectangle.new(canvas, '3c', '4c', '5c', '5c',
'outline' => 'black', 'fill' => 'blue'))
canvas.pack

grp.bind 'Enter' do
grp.configure 'fill', 'red'
end
grp.bind 'Leave' do
grp.configure 'fill', 'green'
end
Tk.mainloop
 
J

Joe Van Dyk

From: Joe Van Dyk <[email protected]>
Subject: Re: tk canvas question
Date: Wed, 22 Jun 2005 06:15:41 +0900
e'))
=20
Please use TkcGroup#include.
For example,
-----------------------------------------------
require "tk"
=20
canvas =3D TkCanvas.new
grp =3D TkcGroup.new(canvas)
grp.include(TkcRectangle.new(canvas, '1c', '2c', '3c', '3c',
'outline' =3D> 'black', 'fill' =3D> 'blue'),
TkcRectangle.new(canvas, '3c', '4c', '5c', '5c',
'outline' =3D> 'black', 'fill' =3D> 'blue'))
canvas.pack
=20
grp.bind 'Enter' do
grp.configure 'fill', 'red'
end
grp.bind 'Leave' do
grp.configure 'fill', 'green'
end
Tk.mainloop

Thank you. How does TkcGroup#include differ from TkcGroup#add?
 
J

Joe Van Dyk

From: Joe Van Dyk <[email protected]>
Subject: Re: tk canvas question
Date: Wed, 22 Jun 2005 06:15:41 +0900
e'))
=20
Please use TkcGroup#include.
For example,
-----------------------------------------------
require "tk"
=20
canvas =3D TkCanvas.new
grp =3D TkcGroup.new(canvas)
grp.include(TkcRectangle.new(canvas, '1c', '2c', '3c', '3c',
'outline' =3D> 'black', 'fill' =3D> 'blue'),
TkcRectangle.new(canvas, '3c', '4c', '5c', '5c',
'outline' =3D> 'black', 'fill' =3D> 'blue'))
canvas.pack
=20
grp.bind 'Enter' do
grp.configure 'fill', 'red'
end
grp.bind 'Leave' do
grp.configure 'fill', 'green'
end
Tk.mainloop

How can I hide and/or destroy all TkCanvas items that are in a group?
 
J

Joe Van Dyk

=20
How can I hide and/or destroy all TkCanvas items that are in a group?

Apparently, I can just delete the group. I'm not sure why it didn't
work before, but it is now.
 
H

Hidetoshi NAGAI

From: Joe Van Dyk <[email protected]>
Subject: Re: tk canvas question
Date: Thu, 23 Jun 2005 01:35:29 +0900
Message-ID: said:
Thank you. How does TkcGroup#include differ from TkcGroup#add?

Although I don't remember the specification of old TkcGroup#add
(I think that was included in Ruby-1.0 or Ruby-1.1b),
probably there is no difference.
So, for users who read such very old document,
I'll add an alias TkcGroup#add --> TkcGroup#include.
 
H

Hidetoshi NAGAI

From: Joe Van Dyk <[email protected]>
Subject: Re: tk canvas question
Date: Thu, 23 Jun 2005 04:03:26 +0900
Message-ID: said:
Apparently, I can just delete the group. I'm not sure why it didn't
work before, but it is now.

The reason is that "add" method call adds no canvas-tag to target
widgets.

TkcGroup has no "add" instance method.
Then, Ruby/Tk assumes that it is configuration of "-add" option.
Of course, canvas items don't have such option.
However, if the target of configuration is a canvas-tag which is not
assigned to any items, Tk generates no error and returns an empty
string.
So, TkcGroup#add does nothing and returns "" without errors.

TkcGroup#include works properly, that is, adds its canvas-tag to
target widgets.
After that, TkcGroup#add will raise an exception 'unknown option
"-add"'.
 
J

Joe Van Dyk

From: Joe Van Dyk <[email protected]>
Subject: Re: tk canvas question
Date: Thu, 23 Jun 2005 04:03:26 +0900

=20
The reason is that "add" method call adds no canvas-tag to target
widgets.
=20
TkcGroup has no "add" instance method.
Then, Ruby/Tk assumes that it is configuration of "-add" option.
Of course, canvas items don't have such option.
However, if the target of configuration is a canvas-tag which is not
assigned to any items, Tk generates no error and returns an empty
string.
So, TkcGroup#add does nothing and returns "" without errors.
=20
TkcGroup#include works properly, that is, adds its canvas-tag to
target widgets.
After that, TkcGroup#add will raise an exception 'unknown option
"-add"'.

Very informative. Thank you!
 

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

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top