[Tutor] Tkinter Canvas Widget

John Fouhy john at fouhy.net
Tue Nov 13 04:59:30 CET 2007


On 13/11/2007, Johnston Jiaa <oclbdk at gmail.com> wrote:
> I'm trying to create a free-hand drawing area on my program using the
> Tkinter canvas widget.  I have read through all the methods on Canvas
> objects <http://infohost.nmt.edu/tcc/help/pubs/tkinter/canvas-
> methods.html>, but do not see how to get the coordinates of the mouse
> on the canvas.
>
> Also, after I get those coordinates, which methods would be
> appropriate to draw the points onto the canvas itself?

It's been a while since I used Tkinter, but from memory:

To get the mouse coordinates, you'll need to use events.  Check out
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm for some
details.

Essentially, you'll need to bind a function to the B1-Motion event,
and then inspect the event object to find out the mouse location.

(I'm assuming here that you only want to draw your lines when the user
has a button depressed..)

To draw points, you could perhaps use canvas.create_oval with a small
bounding box.

HTH!

-- 
John.


More information about the Tutor mailing list