top of page

How to implement Keyboard and Mouse click event in tkinter | Tkinter event handling | Event and Bind

Whenever we use Tkinter, we are not just limited to bind one function to one widget. With widgets, we can bind multiple events to it depending on what we do with that widget, it behaves in different kind of ways. We can have a button, and if we left click it, something happens and if you right click it, something else happens.

Tkinter also lets you install callables to call back when needed to handle a variety of events. However, Tkinterdoes not let you create your own custom events; you are limited to working with events predefined by Tkinter itself.

Tkinter provides a powerful mechanism to let you deal with events yourself. For each widget, you can bind Python functions and methods to events.

widget.bind(event, handler)

If an event matching the event description occurs in the widget, the given handler is called with an object describing the event.

1- The Event Object: General event callbacks must accept one argument event that is a Tkinter event object. Such an event object has several attributes describing the event:

widget, x_root , y_root, x, y, num, keysym, char

2- Binding Callbacks to Events:

Usually enclosed in angle brackets ('<...>')

3- Event Names:

Frequently used event names, which are almost all enclosed in angle brackets, fall into a few categories.

1-Keyboard events

2-Mouse event,

Keyboard Event:

Key, Special keys, Normal keys, Button-1, Button-2, Button-3, B1-Motion, B2-Motion, B3-Motion, ButtonRelease-1, ButtonRelease-2, ButtonRelease-3, Double-Button-1, Double-Button-2, Double-Button-3, Enter, Leave etc

4-Event-Related Methods:

Each widget w supplies the following event-related methods.



w.bind(event_name,callable) sets callable as the callback for event_name on w. w.bind(event_name,callable,'+') adds callable to the previous bindings for event_name on w.



w.bind_all(event_name,callable) sets callable as the callback for event_name on any widget of the application, whatever widget w you call the method on. w.bind_all(event_name,callable,'+') adds callable to the previous bindings for event_name on any widget.



Removes all callbacks for event_name on w.



Removes all callbacks for event_name on any widget, previously set by calling method bind_all on any widget.



Handling Mouse event

-------------------------------------------------------------------------------------------from tkinter import * var = Tk() def leftclick(event): print("left") def middleclick(event): print("middle") def rightclick(event): print("right") frame = Frame(var, width=300, height=250) frame.bind("<Button-1>", leftclick) frame.bind("<Button-2>", middleclick) frame.bind("<Button-3>", rightclick) frame.pack() var.mainloop()


Example 2:

Capturing keyboard events


from tkinter import *

root = Tk()

def key(event): print("pressed", repr(event.char))

def callback(event): frame.focus_set() print("clicked at", event.x, event.y)

frame = Frame(root, width=100, height=100) frame.bind("<Key>", key) frame.bind("<Button-1>", callback) frame.pack()



Click here for any tkinter GUI related assignment help or any programming assignment help.

2,949 views0 comments
bottom of page