The Wayback Machine - https://web.archive.org/web/20240608021754/https://www.geeksforgeeks.org/python-binding-function-in-tkinter/
Open In App

Python | Binding function in Tkinter

Last Updated : 10 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Tkinter is a GUI (Graphical User Interface) module that is widely used in desktop applications. It comes along with the Python, but you can also install it externally with the help of pip command. 
It provides a variety of Widget classes and functions with the help of which one can make our GUI more attractive and user-friendly in terms of both looks and functionality.
The binding function is used to deal with the events. We can bind Python’s Functions and methods to an event as well as we can bind these functions to any particular widget.
Code #1: Binding mouse movement with tkinter Frame. 
 

Python3




# Import all files from
# tkinter and overwrite
# all the tkinter files
# by tkinter.ttk
from tkinter import *
from tkinter.ttk import *
 
# creates tkinter window or root window
root = Tk()
root.geometry('200x100')
 
# function to be called when mouse enters in a frame
def enter(event):
    print('Button-2 pressed at x = % d, y = % d'%(event.x, event.y))
 
# function to be called when mouse exits the frame
def exit_(event):
    print('Button-3 pressed at x = % d, y = % d'%(event.x, event.y))
 
# frame with fixed geometry
frame1 = Frame(root, height = 100, width = 200)
 
# these lines are showing the
# working of bind function
# it is universal widget method
frame1.bind('<Enter>', enter)
frame1.bind('<Leave>', exit_)
 
frame1.pack()
 
mainloop()


Output: 
 

  
Code #2: Binding Mouse buttons with Tkinter Frame 
 

Python3




# Import all files from
# tkinter and overwrite
# all the tkinter files
# by tkinter.ttk
from tkinter import *
from tkinter.ttk import *
 
# creates tkinter window or root window
root = Tk()
root.geometry('200x100')
 
# function to be called when button-2 of mouse is pressed
def pressed2(event):
    print('Button-2 pressed at x = % d, y = % d'%(event.x, event.y))
 
# function to be called when button-3 of mouse is pressed
def pressed3(event):
    print('Button-3 pressed at x = % d, y = % d'%(event.x, event.y))
 
## function to be called when button-1 is double clocked
def double_click(event):
    print('Double clicked at x = % d, y = % d'%(event.x, event.y))
 
frame1 = Frame(root, height = 100, width = 200)
 
# these lines are binding mouse
# buttons with the Frame widget
frame1.bind('<Button-2>', pressed2)
frame1.bind('<Button-3>', pressed3)
frame1.bind('<Double 1>', double_click)
 
frame1.pack()
 
mainloop()


Output: 
 

  
Code #3: Binding keyboard buttons with the root window (tkinter main window). 
 

Python3




# Import all files from
# tkinter and overwrite
# all the tkinter files
# by tkinter.ttk
from tkinter import *
from tkinter.ttk import *
 
# function to be called when
# keyboard buttons are pressed
def key_press(event):
    key = event.char
    print(key, 'is pressed')
 
# creates tkinter window or root window
root = Tk()
root.geometry('200x100')
 
# here we are binding keyboard
# with the main window
root.bind('<Key>', key_press)
 
mainloop()


Output: 
 

Note: When we bind keyboard buttons with the tkinter window, whenever we press special characters we will only get space while in the case of alphabets and numerical we will get actual values (in the string).
 



Previous Article
Next Article

Similar Reads

Binding Function with double click with Tkinter ListBox
Prerequisites: Python GUI – tkinter, Python | Binding function in Tkinter Tkinter in Python is GUI (Graphical User Interface) module which is widely used for creating desktop applications. It provides various basic widgets to build a GUI program. To bind Double click with Listbox we use Binding functions in Python and then we execute the required a
1 min read
How to activate Tkinter menu and toolbar with keyboard shortcut or binding?
You might have seen the menubar and toolbar in the various desktop apps, which are opened through shortcut keys. Don’t you know how to create such a menubar and toolbar which gets opened through a shortcut key? Have a read at the article and get to know the procedure to do the same. For activating the menubar and toolbar with the shortcut key, crea
3 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Python - Binding and Listening with Sockets
Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. Note: For more information, refer Socket Programmi
3 min read
Python | askopenfile() function in Tkinter
While working with GUI one may need to open files and read data from it or may require to write data in that particular file. One can achieve this with the help of open() function (python built-in) but one may not be able to select any required file unless provides a path to that particular file in code. With the help of GUI, you may not require to
2 min read
Python | asksaveasfile() function in Tkinter
Python provides a variety of modules with the help of which one may develop GUI (Graphical User Interface) applications. Tkinter is one of the easiest and fastest way to develop GUI applications. While working with files one may need to open files, do operations on files and after that to save file. asksaveasfile() is the function which is used to
2 min read
Change color of button in Python - Tkinter
Prerequisite: Creating a button in tkinter, Python GUI – tkinter In this article, we are going to write a Python script to change the color of the button in Tkinter. It can be done with two methods: Using bg properties.Using activebackground properties. Example 1: using bg properties. We can change the button background color with bg properties, Th
1 min read
Color game using Tkinter in Python
TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games. Let's try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds. Colors used in this
4 min read
Python | Message Encode-Decode using Tkinter
Prerequisite : Basics of TkinterPython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Python pr
5 min read
Python - Tkinter askquestion Dialog
In Python, There Are Several Libraries for Graphical User Interface. Tkinter is one of them that is most useful. It is a standard interface. Tkinter is easy to use and provides several functions for building efficient applications. In Every Application, we need some Message to Display like "Do You Want To Close " or showing any warning or Something
4 min read
Practice Tags :