How to do it...

The following program contains an empty canvas and a label that shows the location of the cursor on the canvas; you can move the cursor to see what position it is placed in, giving clear feedback on how the x and y coordinates increment or decrement, depending on the direction you move the mouse pointer:

import tkinter as tk

class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Basic canvas")

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

self.canvas.pack()
self.label.pack()

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

if __name__ == "__main__":
app = App()
app.mainloop()
..................Content has been hidden....................

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