Bind in Tkinter: A Complete Guide

Bind in Tkinter

In this article let’s try to understand the concept of bind used in Tkinter. Before starting with this topic, we must remember that Python’s Tkinter package is used to design GUI-based interfaces. Tkinter has a variety of built-in functionalities and extensions that can be used to enhance the functionality and performance of the application.

What is bind?

The basic definition of the word bind is stick together or cause to stick together in a single mass. Similarly, Tkinter bind is used to connect an event passed in the widget along with the event handler. The event handler is the function that gets invoked when the events take place.

widget.bind(sequence=None, func=None, add=None)

The sequence the argument describes what event we expect, and the func argument is a function to be called when that event happens to the widget. If there was already a binding for that event for this widget, normally the old callback is replaced with func, but you can preserve both callbacks by passing add='+'.

The events can be bonded to an event handler using the bind function at different levels.

1. Instance-level binding

One can bind an event to one specific widget. To bind an event of a widget, call the .bind() method on that widget.

widget.bind(event, event handler)
  • Event – occurrence caused by the user that might reflect changes.
  • Event Handler – function in your application that gets invoked when the event takes place.
  • Bind – configuring an event handler (python function) that is called when an event occurs to a widget.

Following is an example of how to bind an event to a particular instance of a widget.

import tkinter as tk
class Display:
    def __init__(self):
        self.root = tk.Tk()
        self.entry1 = tk.Entry(self.root)
        self.entry1.bind("<KeyPress>", self.onKeyPress)
        self.entry1.pack()
        self.root.mainloop()

    def onKeyPress(self, event):
        print("Key has been Pressed.")
display = Display()

Output

Implementation
Implementation

Multiple Binding

What if we need to bind more than one function to a particular widget? Passing two statements containing the bind the function will certainly not work because the second statement will override the first statement hence only the second function will be invoked as shown below

Implementation
Implementation

This problem is solved by adding one extra parameter add to the second statement that uses the bind function. Also do not forget to assign add=”+”. This will invoke both functions. Hence we can enable multiple binding in Tkinter.

Implementation
Implementation

2. Class-level binding

One can bind an event to all widgets of a class. For example, you might set up all Button widgets to respond to middle mouse button clicks by changing back and forth between English and Japanese labels. To bind an event to all widgets of a class, call the .bind_class() method on any widget.

The “class” mentioned in  bind_class refers to the internal class name used by the tk library, not the python class name. bind_class is a method available to all widgets and simply calls the Tk bind command again, however not with the instance name, but the widget class name.

w.bind_class(className, sequence=None, func=None, add=None)

The basic working of .bind_class is the same as the .bind function.

For example, suppose you have several widgets of the same class, here let’s consider we have multiple entry widgets and we need to set all of them to the same function. Rather than having to call .bind() for every one of them, you can set them all up with one call something like this:

from tkinter import *
import tkinter as tk

class Display:
    def __init__(self):
        self.root = tk.Tk()
        self.entry_username = tk.Entry(self.root)
        self.entry_birthday = tk.Entry(self.root)
        self.entry_password= tk.Entry(self.root)
        self.entry_username.bind_class("Entry", "<Return>", self.onReturn)

        self.entry_username.pack()
        self.entry_birthday.pack()
        self.entry_password.pack()

        #to know the class type of the widget use following command.
        #print(self.entry_username.winfo_class())
        self.root.mainloop()

    def onReturn(self, event):
        print("Return pressed")
display = Display()

Output

Implementation
Implementation

3. Application-level binding

One can set up a binding so that a certain event calls a handler no matter what widget has the focus or is under the mouse.

w.bind_all(sequence=None,  func=None,  add=None)

Like .bind(), but applies to all widgets in the entire application.

For example, you might have multiple widgets of the same type throughout the program, they make be part of different classes. To bind an event at the application level, call the .bind_all() method on any widget. While using .bind_all you do not need to mention the class name as the bind is applied on each and every event of the application. Here’s how to implement it,

from tkinter import *
import tkinter as tk

class Display:
    def __init__(self):
        self.root = tk.Tk()
        self.entry_username = tk.Entry(self.root)
        self.entry_birthday = tk.Entry(self.root)
        self.entry_password = tk.Entry(self.root)
        self.entry_username.bind_all("<Return>", self.onReturn)

        self.entry_username.pack()
        self.entry_birthday.pack()
        self.entry_password.pack()

        #to know the class type of the widget use following command.
        print(self.entry_username.winfo_class())
        self.root.mainloop()

    def onReturn(self, event):
        print("Return pressed")
display = Display()

Output

Implementation
Implementation

Summary

In this article, we have understood that Bind is the function used in Tkinter to join or associate an event with a particular function called event handler for a widget. Also, Tkinter supports three levels of binding they are: instance-level binding, class-level binding, and application-level binding.

Reference

https://web.archive.org/web/20190514010732id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html