Tkinter select more than one

s713221 at student.gu.edu.au s713221 at student.gu.edu.au
Mon May 7 07:00:10 EDT 2001


Nickson Fong wrote:
> 
> can someone tell me how i can select more than on canvas object at once
> and move it with the mouse.
> 
> fing_withtag() - allows only one item?
> 
> --
> thanks

find_withtag() will only select one item if you use item id numbers.
However, if you tag items and use that in find_withtag(), you get a
tuple of all items with that tag.

eg.
Python 2.1 (#1, Apr 22 2001, 13:28:57)
[GCC 2.95.3 19991030 (prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> c=Tkinter.Canvas()
>>> c.pack()
>>> line = c.create_line(10,10,100,100)
>>> line
1
>>> c.find_withtag(1)
(1,)
>>> c.itemconfig(1,tags='hello')
>>> line2 = c.create_line(100,10,100,10,tags='hello')
>>> line2
2
>>> c.find_withtag('hello')
(1, 2)

If you're trying to get a dynamic list of items that don't share the
same tags, you'll have to go through the list and tag them one by one,
and then call find_withtag(), 
>>> for i in range(1,3):
...     c.itemconfig(i,tags='goodbye')
...
>>> c.find_withtag('goodbye')
(1, 2)

Trying to get an arbitrary list of tags using a tuple doesn't work.
>>> c.find_withtag((1,2))
()
Neither does trying to pass several arguments.
>>> c.find_withtag(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: find_withtag() takes exactly 2 arguments (3 given)
         
Anycase, in order to move your items, you need move(), not
find_withtag(), and again it accepts item ids or tags.

>>> c.move('hello', 10,10)
>>> 
The second and third arguments are displacements, and in order to get
them from mouse events, you'll need to read the references on events and
function binding. Sorry, still learning these myself.

Joal Heagney/AncientHart



More information about the Python-list mailing list