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()
source share