Event Binding


Binding Mouse events

<Button-1> Mouse Left button click
<Button-2> Mouse center button click
<Button-3> Mouse Right button click
<B1-Motion> Mouse Left button Press and move
<ButtonRelease-1> Mouse Left button release, ButtonRelease-2 for Middle and 3 for right button.
<Double-Button-1> Left Mouse key is double clicked.
<Enter> Mouse Entry over a widget
<Leave> Mouse Leave a widget
<MouseWheel> Mouse Wheel Up or Down rotation

Tkinter binding Mouse button wheel and movement events capturing and triggering callback functions

event.x event.y

Based on any event we can read the x, y coordinates of the position by reading event.x and event.y values. Here is a function which displays the x and y coordinates inside a Label. We used config() to update the text option of the Label.

str() is used to convert integer to string.
def my_callback(event):
     l1.config(text='Clicked at : '+ str(event.x) +","+ str(event.y))

Bind Events

We can bind various mouse events of parent window ( or any widget ) and trigger the callback function. Here is a sample of using left mouse button click to call my_callback() function.
my_w.bind('<Button-1>',my_callback) #Mouse Left button  click
This is same as using <ButtonPress-1> <1> to bind Mouse left button click.
Drag and Drop using Mouse left button press.

Using Lambda

We can directly use lambda to config the text property of Label.
l1.bind('<Enter>',lambda e:l1.config(text='Welcome'))
Full code with all mouse events are here
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("615x400") 
def my_callback(event):
     l1.config(text='Clicked at : '+ str(event.x) +","+ str(event.y))

l1=tk.Label(my_w,text='to Display',bg='yellow',font=('Times',26,'normal'))
l1.grid(row=0,column=1,padx=10,pady=10)

my_w.bind('<Button-1>',my_callback) # Mouse Left button  click <ButtonPress-1><1>

#my_w.bind('<Button-2>',my_callback) # Mouse middle button  click
#my_w.bind('<Button-3>',my_callback) # Mouse right button  click
#my_w.bind('<B1-Motion>',my_callback) # Mouse left button pressed move
#my_w.bind('<ButtonRelease-1>',my_callback) # Mouse left button  release
#my_w.bind('<Double-Button-1>',my_callback) # Mouse left button  Double click 
#l1.bind('<Enter>',lambda e:l1.config(text='Welcome')) # Mouse enters label 
#l1.bind('<Leave>',lambda e:l1.config(text='Thanks')) # Mouse leaves label 
#my_w.bind('<MouseWheel>',my_callback) # Mouse middle button  click
my_w.mainloop()

Keyboard Events

<Key> Any Key is pressed
<Return> Enter Key is pressed
<KeyPress> Key Press event of any widget
<KeyRelease> Key Release event of any widget
<FocusIn> When widget got focus
<FocusOut> When widget lost focus
<Right> When Right arrow is pressed
<Left> When Left arrow is pressed
<Up> When Up arrow is pressed
<Down> When Down arrow is pressed

Tkinter binding keyboard events like Key press, release , focus, enter and callback functions


Callback function to read the Key pressed.
def my_callback(event):
    l1.config(text='You Pressed  : '+ event.char)
Binding the event
my_w.bind("<Key>", my_callback)
Using one-liner for Enter Key
my_w.bind("<Return>", lambda e:l1.config(text='Hi'))

Binding particular Key

To bind the event of specific key
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("615x400") 
def my_callback(event):
     l1.config(text='Clicked the key : '+ event.char)

l1=tk.Label(my_w,text='to Display',bg='yellow',font=('Times',26,'normal'))
l1.grid(row=0,column=1,padx=10,pady=10)

my_w.bind('<a>',my_callback) # Key a is pressed
my_w.bind('<K>',my_callback) # Key K is pressed

my_w.mainloop()

Difference between KeyPress and KeyRelease

Here the function my_t1() change the background colour of Entry widget t1 once the number of char exceeds 5. Here the KeyPress event will react after 7 chars are entered. However KeyRelease event will trigger the change in background colour once 6th char is entered.
Difference between KeyPress and KeyRelease events.
def my_t1(*args):
    if(len(t1.get())>5):
        t1.config(bg='red')
    else:
        t1.config(bg='lightgreen')
Full code is here
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("615x400")
font=('Times',26,'normal')  
def my_callback(event):
    l1.config(text='You Pressed  : '+ event.char)
def my_t1(*args):
    if(len(t1.get())>5):
        t1.config(bg='red')
    else:
        t1.config(bg='lightgreen')

l1=tk.Label(my_w,text='to Display',bg='yellow',font=font)
l1.grid(row=0,column=0,padx=10,pady=10)
t1=tk.Entry(my_w,bg='lightgreen',font=font)
t1.grid(row=1,column=0,padx=10)

t2=tk.Entry(my_w,bg='lightgreen',font=font)
t2.grid(row=2,column=0,padx=10,pady=10)

my_w.bind("<Key>", my_callback) # Any key is pressed 
my_w.bind("<Return>", lambda e:l1.config(text='Hi')) # Enter Key
t1.bind("<KeyPress>",my_t1)
#t1.bind("<KeyRelease>",my_t1)
t1.bind("<FocusIn>",lambda e:l1.config(text='Welcome'))
t1.bind("<FocusOut>",lambda e:l1.config(text='Thanks'))

my_w.mainloop()

Using Arrow Keys

We can trigger different function by using directional arrow keys.
my_w.bind('<Right>',right)
my_w.bind('<Left>',left)
my_w.bind('<Up>',up)
my_w.bind('<Down>',down)
Triggering on Right arrow key press
my_w.bind("<Right>",lambda e:l1.config(text='Right arrow'))
Typing keys to create Keyboard events . Enter & Leave Mouse events. Drag and Drop using Mouse button. Moving elements in canvas using arrow keys.
More Projects using Tkinter Cut Copy Paste in Entry box Cut Copy Paste in Text box
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer