Re: [Tutor] Newbie struggling with Tkinter/canvas tags
> > Have you tried the addtag_withtag() method? It looks like it should be > > able to do what you're thinking of. The documentation here: > > > > http://tkinter.unpythonic.net/pydoc/Tkinter.Canvas.html > > > > should talk about addtag_withtag(). > > itemconfig() also looks promising. > > http://www.pythonware.com/library/tkinter/introduction/x2017-concepts.htm > Thanks Danny and Kent, I see it now. With so many options on offer it's easy to miss the obvious. All of this new information should keep me occupied for quite some time. Thanks again. Glen ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
Danny Yoo wrote: Now I've just got to work out how to tag a list of id's... There doesn't seem to be a way to tag a known id, it has to be tagged by reference from an id above or below which seems odd! Hi Glen, Have you tried the addtag_withtag() method? It looks like it should be able to do what you're thinking of. The documentation here: http://tkinter.unpythonic.net/pydoc/Tkinter.Canvas.html should talk about addtag_withtag(). itemconfig() also looks promising. http://www.pythonware.com/library/tkinter/introduction/x2017-concepts.htm Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
> Now I've just got to work out how to tag a list of id's... There > doesn't seem to be a way to tag a known id, it has to be tagged by > reference from an id above or below which seems odd! Hi Glen, Have you tried the addtag_withtag() method? It looks like it should be able to do what you're thinking of. The documentation here: http://tkinter.unpythonic.net/pydoc/Tkinter.Canvas.html should talk about addtag_withtag(). ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
On Mon, 2005-01-31 at 18:20, Kent Johnson wrote: > > In 2.4 I tried 'from sets import set' > > should be 'from sets import Set' - the class in the sets module is called > Set, the builtin class is set. > I tried it as 'set' and 'Set' but got the same result > > > > Traceback (most recent call last): > > File "/home/glen/set4.py", line 1, in -toplevel- > > from sets import set > > ImportError: cannot import name set > > > > It did allow 'from sets import *' but when using the set command > > I got some odd output... > > > > from sets import * > > a=[12,54,67,47,98,76] > > b=[47,54] > > print set(a)-set(b) > > > > > > set(['dog','sheep,'cow']) > > set([76, 98, 67, 12]) > > ?? Where did the animals come from?? That's the odd output, that was from a previous set command, even after a reboot it still came out the same. Inbuilt set works fine. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
On Mon, 2005-01-31 at 09:06, Danny Yoo wrote > > 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]) > ### Thanks Danny, sets work great (inbuilt for 2.4) I'd never heard of them before today. I look forward to finding other uses for them. Now I've just got to work out how to tag a list of id's... There doesn't seem to be a way to tag a known id, it has to be tagged by reference from an id above or below which seems odd! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
On Mon, 2005-01-31 at 12:34, Kent Johnson wrote: > Kent Johnson wrote: > > Note that Python 2.4 has set built-in with the name 'set'. To be > > compatible with both you could write > > try: > > set > > except NameError: > > from sets import Set as set > > Clarification: you don't _have_ to do this to be compatible with 2.4. The sets module is in both 2.3 > and 2.4. The difference in 2.4 is that set is also a built-in data type implemented in C with the > same interface as sets.Set. The code above will take advantage of the built-in set if it is > available, otherwise use sets.Set. > In 2.4 I tried 'from sets import set' Traceback (most recent call last): File "/home/glen/set4.py", line 1, in -toplevel- from sets import set ImportError: cannot import name set It did allow 'from sets import *' but when using the set command I got some odd output... from sets import * a=[12,54,67,47,98,76] b=[47,54] print set(a)-set(b) >>> set(['dog','sheep,'cow']) set([76, 98, 67, 12]) >>> It seems to save the output from the last use, and reproduces it later, even after a system reboot I got the same output. Using the inbuilt commands works fine. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
Kent Johnson wrote: Note that Python 2.4 has set built-in with the name 'set'. To be compatible with both you could write try: set except NameError: from sets import Set as set Clarification: you don't _have_ to do this to be compatible with 2.4. The sets module is in both 2.3 and 2.4. The difference in 2.4 is that set is also a built-in data type implemented in C with the same interface as sets.Set. The code above will take advantage of the built-in set if it is available, otherwise use sets.Set. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
Danny Yoo wrote: 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. ?? Sets can contain any object that is usable as a dictionary key. 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 Note that Python 2.4 has set built-in with the name 'set'. To be compatible with both you could write try: set except NameError: from sets import Set as 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 ### or all_objects = Set(canvas.find_all()) all_objects.difference_update(canvas.find_withtag(tag)) return all_objects Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
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! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie struggling with Tkinter/canvas tags
On Sun, 30 Jan 2005, Glen wrote: > As a Python/Tkinter newbie, I thought I was getting on ok... > then I hit this problem. > > I have a canvas (c1) > A group of objects are drawn on c1 and given a tag > c1.addtag_all('group_A') > Another group of objects are drawn, I wish to tag these 'group_B'. Hi Glen, Ok, so you're looking for something like a hypothetical "find_withouttag" to define 'group_B' then. I don't think such a function exists directly, but we can make it up. > At the moment I 'get by' with... > > a=c1.find_withtag('group_A') > b=c1.find_all() > c=b[len(a):] > for i in range(len(c)): > c1.addtag_above('group_B',len(a)+i) There's one problem here: the system doesn't guarantee that all the 'group_A' elememts will end up at the beginning of 'b', so there's a very strong possibility of tagging the wrong elements here. I think that getting this right will take some more work. Here's a definition of a function called find_withouttag(): ### def find_withouttag(canvas, tag): """Returns all the objects that aren't tagged with 'tag'.""" all_objects = canvas.find_all() tagged_objects = canvas.find_withtag(tag) return subtract_instances(all_objects, tagged_objects) def subtract_instances(l1, l2): """Returns a list of all the object instances in l1 that aren't in l2.""" identity_dict = {} for x in l1: identity_dict[id(x)] = x for x in l2: if id(x) in identity_dict: del identity_dict[id(x)] return identity_dict.values() ### [Side note to other: the work that subtract_instances() does is similar to what IdentityHashMap does in Java: does anyone know if there's already an equivalent dictionary implementation of IdentityHashMap in Python?] We can see how subtract_instances() works on a simple example: ### >>> class MyClass: ... def __init__(self, name): ... self.name = name ... def __repr__(self): ... return str(self.name) ... >>> frodo, elrond, arwen, aragorn, sam, gimli = ( ...map(MyClass, "Frodo Elrond Arwen Aragorn Sam Gimli".split())) >>> hobbits = frodo, sam >>> everyone = frodo, elrond, arwen, aragorn, sam, gimli >>> >>> subtract_instances(everyone, hobbits) [Arwen, Aragorn, Gimli, Elrond] ### This subtract_instances() function should do the trick with instances of the canvas items. There's probably a simpler way to do this --- and there are things we can do to make it more efficient --- but I have to go to sleep at the moment. *grin* Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor