Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 9 - GUI Programming using Tkinter - Section 9.11 - Mouse, Key Events, and Bindings - Check Point - MyProgrammingLab - Page 297: 9.35

Answer

In Tkinter, the current mouse location can be obtained from the event object passed to the event-handling function by using the x and y attributes of the event object.

Work Step by Step

The code below shows an example of how to retrieve the mouse location when the mouse is clicked: def get_mouse_location(event): $\hspace{4mm}$x = event.x $\hspace{4mm}$y = event.y $\hspace{4mm}$print("Mouse clicked at", x, y) canvas.bind("", get_mouse_location) In this example, the get_mouse_location function is called whenever the left mouse button is clicked. The x and y attributes of the event object contain the mouse coordinates.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.