How it works...

The Canvas instance is created like any other Tkinter widget, that is, by first passing the parent container and the additional configuration options as keyword arguments:

    def __init__(self):
# ...
self.canvas = tk.Canvas(self, bg="white")
self.label = tk.Label(self)
self.canvas.bind("<Motion>", self.mouse_motion)

The next screenshot shows a point composed of the perpendicular projections of each axis:

  • The x coordinate corresponds to the distance on the horizontal axis and increments its value when you move the cursor from left to right
  • The y coordinate is the distance on the vertical axis and increments its value when you move the cursor from up to down

As you might have noticed in the preceding screenshot, these coordinates directly map to the x and y attributes of the event instance passed to the handler:

    def mouse_motion(self, event):
x, y = event.x, event.y
text = "Mouse position: ({}, {})".format(x, y)
self.label.config(text=text)

This happens because these attributes are calculated in respect to the widget that the event is bound to, in this case, the <Motion> sequence.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset