[Tutor] Newbie struggling with Tkinter/canvas tags
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Jan 31 10:06:18 CET 2005
On Mon, 31 Jan 2005, Danny Yoo wrote:
> I think that getting this right will take some more work. Here's a
> definition of a function called find_withouttag():
[Code cut]
Oh! Never mind; this can be a lot simpler. According to the "Gotchas"
section of:
http://tkinter.unpythonic.net/wiki/Widgets/Canvas
the items in a Canvas are actually not object instances themselves, but
integers. I made an assumption that canvas items were object instances,
so I wrote that subtract_instances() to take care of issues with them.
But if canvas items are really integers, then we don't need
subtract_instances() at all. We can just use sets and subtract one set
from the other. Here is a redefinition of find_withouttag() which should
work better:
###
from sets import Set
def find_withouttag(canvas, tag):
"""Returns all the objects that aren't tagged with 'tag'."""
all_objects = Set(canvas.find_all())
tagged_objects = Set(canvas.find_withtag(tag))
return all_objects - tagged_objects
###
Here's a small example with sets to make it more clear how this set
manipulation stuff will work:
###
>>> from sets import Set
>>> numbers = range(20)
>>> primes = [2, 3, 5, 7, 11, 13, 17, 19]
>>> Set(numbers) - Set(primes)
Set([0, 1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18])
###
For more information on sets, see:
http://www.python.org/doc/lib/module-sets.html
Best of wishes to you!
More information about the Tutor
mailing list