Python | Binding function in Tkinter

Tkinter is a GUI (Graphical User Interface) module which is widely used in the desktop applications. It comes along with the Python, but you can also install it externally with the help of pip command.
It provides 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.

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 when mouse exits the frame
def exit_(event):
    print('Button-3 pressed at x = % d, y = % d'%(event.x, event.y))
  
# frame with fixed geomerty
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()

chevron_right


Output:


 
Code #2: Binding Mouse buttons with tkinter Frame

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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>', lambda a : key_press(a))
  
mainloop()

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.