Tkinter canvas drag/drop obstacle

Simon Forman sajmikins at gmail.com
Mon Jun 23 06:53:28 CEST 2008


On Jun 22, 7:41 pm, Peter Pearson <ppear... at nowhere.invalid> wrote:
> On Fri, 20 Jun 2008 13:41:35 -0300, Guilherme Polo <ggp... at gmail.com> wrote:
> > On Fri, Jun 20, 2008 at 1:11 PM, Peter Pearson <ppear... at nowhere.invalid> wrote:
> >> Tkinter makes it very easy to drag jpeg images around on a
> >> canvas, but I would like to have a "target" change color when
> >> the cursor dragging an image passes over it.  I seem to be
> >> blocked by the fact that the callbacks that might tell the
> >> target that the mouse has entered it (<Enter>, <Any-Enter>,
> >> even <Motion>) aren't called if the mouse's button is down.
> >> What am I missing?  Have I failed to find the right Tkinter
> >> document?  Is Tkinter the wrong tool for this job?  Thanks.
>
> > I believe the only way to achieve this is binding <Motion> to the
> > entire canvas, then checking if the x, y coords are inside the
> > "target".
>
> Ugh.  OK, thanks.
>
> --
> To email me, substitute nowhere->spamcop, invalid->net.


Yep, but it's not so bad:

from Tkinter import *

c = Canvas()
c.pack()
i = c.create_oval(1, 1, 100, 100, fill='green')

def cb(e):
    items = c.find_overlapping(
        e.x, e.y,
        e.x + 1, e.y + 1
        )
    if not items:
        return
    print items

c.bind("<B1-Motion>", cb)

mainloop()




The c.find_overlapping() method returns a tuple.  I believe the item
ids in the tuple (if any) will be in the same order as the items Z
order on the canvas, so "items[-1]" should always be the "topmost"
graphic item (again, if any.)



More information about the Python-list mailing list