Entry & Leave Events

Event Mouse Entry and Leave

When Mouse enters or hovers over any widget we can trigger any call back function. Similarly when mouse moves out or Leave the widget we can trigger another call back function.

Tkinter Mouse Enter & Leave events to trigger function to manage the attributes using config()

Using config()

To change any attribute of the widget with the event we are using config() method. Several properties like text, font, background color etc can be changed inside the call back function.

Call back function

Here is one call back function to change the background color and text of a Button and a Label.
def my_callback1(event):
    l2.config(text='Click to register',bg='lightyellow')
Above call back function is triggered when mouse enters over the Button ( b1 )
Full code is here
import tkinter as tk
my_w = tk.Tk()

def my_callback1(event):
    l2.config(text='click this')
def my_callback2(event):
    b1.config(text='Thanks ',bg='gray')
    l2.config(text='You left the button')

def my_callback3(event):
def my_callback4(event):
    l1.config(text='Thanks ',bg='lightblue')    

b1=tk.Button(my_w,text='I am a Button',font=22)
str1=tk.StringVar(value='I am entry ')

l1=tk.Label(my_w,text='I am Label',font=22,bg='lightgreen')

l2=tk.Label(my_w,text='Message area',font=22,bg='yellow')

b1.bind('<Enter>',my_callback1) # Mouse enters the button
b1.bind('<Leave>',my_callback2) # Mouse leaves the button
e1.bind('<Enter>',lambda event:str1.set('Data here')) # entry
l1.bind('<Enter>',my_callback3) # Mouse enters the Label 
l1.bind('<Leave>',my_callback4) # Mouse leaves the Label 

Mouse enter & Leave event over a Label

Mouse entry and leave event on Tkiner Label to update attributes by using config()

We used one Label here
import tkinter as tk
my_w = tk.Tk() # Root window 
my_w.geometry("300x180")  # width and height of the window 

l1 = tk.Label(my_w,  text='plus2net', width=15,font=26,bg='yellow' ) 

l1.bind('<Enter>',lambda e:l1.config(text='Welcome',bg='lightgreen'))
l1.bind('<Leave>',lambda e:l1.config(text='Thanks',bg='yellow'))

my_w.mainloop() # Keep the window open 
Moving elements using arrow keys. Tkinter Canvas Animation using Rectangles & Circles
Sin & cos curves in Canvas More Projects using Tkinter
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    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