How to bind click event with Canvas in Tkinter?

I'm just wondering if there is any possible way to bind the click event to a canvas in Tkinter.

I would like to be able to click anywhere on the canvas and move the object to it. I can make a move, but I did not find a way to bind a click event on the canvas.

+6
source share
1 answer

Taken directly from the example from the Effbot event tutorial .

In this example, we use the frame widget binding method to bind the callback function to the called event. Run this program and click in the window that appears. Each time you click, a message such as "click on 44 63" is printed in the console window. Keyboard events are sent to the widget that currently owns the keyboard focus. You can use the focus_set method to move the focus to the widget:

from Tkinter import * root = Tk() def key(event): print "pressed", repr(event.char) def callback(event): print "clicked at", event.x, event.y canvas= Canvas(root, width=100, height=100) canvas.bind("<Key>", key) canvas.bind("<Button-1>", callback) canvas.pack() root.mainloop() 
+10
source

Source: https://habr.com/ru/post/984130/


All Articles