Introduction to Tkinter Bind
In Tkinter, bind is defined as a Tkinter function for binding events which may occur by initiating the code written in the program and to handle such events occurring in the program are handled by the binding function where Python provides a binding function known as bind() where it can bind any Python methods and functions to the events. Therefore, in general, the Tkinter bind is defined as a function for dealing with the functions and methods of Python that are bind to the events that occur during the program execution such as moving the cursor using a mouse, clicking of mouse buttons, clicking of buttons on the keyboard, etc are events that are handled using bind function in Tkinter.
Working of Tkinter Bind in Python
In this article, we will discuss the Tkinter binding function that is usually used to bind any Python functions and methods to the events or actions that occur by initiating the out of box program or scope of the program and such actions can be handled by a small piece of code and this code is bind function in Tkinter. In Tkinter which is usually used for designing the web applications or desktop applications where there are different events created such as navigating from one page to another, clicking on one link and going to another page, etc are all events and such events can be handled by using a binding function known as bind() which we can bind any functions of Python to such events to handle the events properly and then these functions, in turn, can be bind with any widgets provided by the Tkinter module. Therefore events handling is not only done with the bind() function we have already used or seen when using the button widget, where on clicking the button widget we would get some message which is already written in the command option of the button widget. In the same way, the bind function is used to deal with some other events that are occurred through the system by the users some of the common events are like clicking the mouse button, pressing of any button on the keyboard, etc.
In this article, let us see a few examples of using the bind function to deal with a few system events like mouse clicks, keyboard button typing, or pressing. So first we will see a syntax where each widget provided we can bind it with Python methods which are possible using the bind() function.
Syntax:
widget.bind(event, handler)
Parameters:
- event: this parameter is used to define the events so that it can be handled using the handler function. E.g FocusIn, Enter, KeyPress, etc
- handler: this parameter is used to define the handler function so that it can describe the event that occurred using the event objects which are called along with the handler function such as having a mouse position using the x and y-axis in pixels, mouse button numbers, etc.
So using the above syntax we can bind an event to a function with a very simple process which is just if any event such as clicking the mouse or keyboard buttons an event occurs which will automatically trigger the handler function defined in the syntax and suppose if the event argument is not given or hidden in the function then there is a chance of you getting an error known as TypeError. There are other defined events provided by Tkinter such as the <Destroy> event which when defined or specified or bind with any widget then the widget is destroyed, therefore there are many such events that can be seen in the Tkinter module itself.
Now let us see an example of how to use the bind function by attaching the event to the widgets. In the below example we will see a keyboard button typing along with the key which is pressed is displayed.
Example
Code:
from Tkinter import *
import tkMessageBox
master = Tk()
master.geometry('500x200')
def func():
tkMessageBox.showinfo( "Hello Educba", "Press any key on the keyboard")
b1 = Button( master, text='Click me for next step', background = 'Cyan', fg = '#000000', command = func)
b1.pack()
def Keyboardpress( key):
key_char = key.char
print( key_char, 'key button is pressed on the keyboard')
master.bind( '', lambda i : Keyboardpress(i))
master.mainloop()
In the above program, we have seen a simple code for seeing the key pressed on the keyboard and this event <key> is bind with the Python function which we have defined in this program is “Keyboardpress()” where when this event is occurred then automatically when we type or press any key on the keyboard it prints the key name which you have typed on the output screen as shown in the above screenshot. In this above code, we have also seen the button widget calling an event by specifying the action to do in the function defined as a value to the command option in the button widget. Therefore we can see in the above first, when we run the program it will display a button having the name as “click me for the next step” so when we click this button an event is generated, and the function defined in the command option is triggered by handling this event which shows a message saying “Press any key on the Keyboard”. Then this pressing of any key is again an event <key> which we have bind this event with the function defined in the program to display the characters pressed on the keyboard which is done by using the bind() function.

4.5 (6,445 ratings)
View Course
Therefore there are many such events like <destroy> for destroying the widgets, <configure> for setting the window display according to the parent window, <key> for pressing the keys on the keyboard, <Enter> for pressing on the enter key on the keyboard, etc. These all events we can bind with the function which we define in the program and handle such events.
Conclusion
In this article, we conclude that the Tkinter bind is a binding function for binding any event to the Python function or method that is defined within the program to handle such events. In this article, we saw how the bind() function can be define using the syntax that holds both the event name and the handler which would be the function name. In this article, we also saw a simple example of pressing any key on the keyboard is an event and the pressed key names are displayed on the output screen.
Recommended Articles
This is a guide to Tkinter Bind. Here we discuss the Introduction and working of Tkinter Bind along with an example and code implementation. You may also have a look at the following articles to learn more –