Tkinter canvas drag/drop obstacle

Matimus mccredie at gmail.com
Fri Jun 20 23:10:27 CEST 2008


On Jun 20, 11:10 am, Matimus <mccre... at gmail.com> wrote:
> On Jun 20, 9:11 am, 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.
>
> > --
> > To email me, substitute nowhere->spamcop, invalid->net.
>
> I have used a combination of <Motion> and <B1-Motion>. You might also
> throw in a <Button-1> event to keep track of whether or not the mouse
> button was down when it entered the widget or not.
>
> Depending on what you really want to do though, you might take
> advantage of the 'active' state:
>
> import Tkinter as tk
>
> can = tk.Canvas()
> can.pack(fill=tk.BOTH, expand=True)
>
> can.create_rectangle(
>         10,10,100,100,
>         fill="black",
>         activewidth=5,
>         activeoutline="blue"
>         )
>
> can.mainloop()
>
> The 'active*' options take effect when the mouse is on top of that
> item.
>
> If all you are _really_ interested in is a visual indicator, this
> should work for you. Note that there is also a disabled state. I only
> discovered this by looking at the options available and guessing.
>
> >>> from pprint import pprint
> >>> import Tkinter as tk
> >>> can = tk.Canvas()
> >>> can.pack(fill=tk.BOTH, expand=True)
> >>> r = can.create_rectangle(10,10,100,100)
> >>> pprint(can.itemconfig(r))
>
> {'activedash': ('activedash', '', '', '', ''),
>  'activefill': ('activefill', '', '', '', ''),
>  'activeoutline': ('activeoutline', '', '', '', ''),
>  'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''),
>  'activestipple': ('activestipple', '', '', '', ''),
>  'activewidth': ('activewidth', '', '', '0.0', '0.0'),
>  'dash': ('dash', '', '', '', ''),
>  'dashoffset': ('dashoffset', '', '', '0', '0'),
>  'disableddash': ('disableddash', '', '', '', ''),
>  'disabledfill': ('disabledfill', '', '', '', ''),
>  'disabledoutline': ('disabledoutline', '', '', '', ''),
>  'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''),
>  'disabledstipple': ('disabledstipple', '', '', '', ''),
>  'disabledwidth': ('disabledwidth', '', '', '0.0', '0'),
>  'fill': ('fill', '', '', '', ''),
>  'offset': ('offset', '', '', '0,0', '0,0'),
>  'outline': ('outline', '', '', 'black', 'black'),
>  'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'),
>  'outlinestipple': ('outlinestipple', '', '', '', ''),
>  'state': ('state', '', '', '', ''),
>  'stipple': ('stipple', '', '', '', ''),
>  'tags': ('tags', '', '', '', ''),
>  'width': ('width', '', '', '1.0', '1.0')}
>
> The 'state' option can be set to 'normal', 'hidden' or 'disabled'. So
> if you want to make your canvas items look different when they are
> disabled, set the disabled* options and set 'state' to 'disabled'.
>
> Matt

I appologize. I didn't actually test this before posting the code, but
if you have the mouse button down before entering an item on the
canvas, even the active state doesn't seem apply. So, well, I hope
someone finds this information useful, but I guess it isn't going to
solve the original posters issue.

Matt



More information about the Python-list mailing list